Fastest way to extend to a class in another package, with the same name as one in the current package

StackOverflow https://stackoverflow.com/questions/21403891

  •  03-10-2022
  •  | 
  •  

I have in my package two classes: Animal and AnimalTest. I have in a different package another class Animal.

I want to have

public class AnimalTest extends Animal

where Animal is the class from the other package. But when I write that, Eclipse automatically selects the class Animal from my current package.

I found to ways to get what I want:

  • by deleting the file, creating a class with the wizard and indicating the path of the Superclass
  • writing:

    public class AnimalTest extends com.the.other.package.Animal

  • then writing:

    import com.the.other.package.Animal

  • then changing the header back to:

    public class AnimalTest extends Animal

and this time it is linked to the Animal class I want.

This seems very cumbersome to me and I am sure there is a better and simpler way to do it?

有帮助吗?

解决方案

Press Ctrl+Space after extends Animal.

Eclipse will display both classes in a dialog. Select the one you want and Eclipse will change the sources accordingly to make it happen.

If you want to insert the full name, double click on the Java type (i.e. Animal), open the context menu, press Y (Copy Qualified Name) and Ctrl+V.

其他提示

I suggest you should use public class AnimalTest extends com.the.other.package.Animal because if you have two classes with same name in two different packages, and trying to use

import com.the.other.package.Animal
public class AnimalTest extends Animal

then your JVM should consider this Animal class as your current package class, not from the package which you import.

@George : here is the answer:

Suppose you have two classes: Animal & AnimalTest in com.one package. And another Animal class is in com.two package. Now if you want to extend Animal class of com.two package then simply you need to code like this:

class AnimalTest extends com.two.Animal{
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top