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:
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?
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.
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 (ie Animal
), open the context menu, press Y (Copy Qualified Name) and Ctrl+V .
@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{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.