I'm sure this is incredibly common with as OOP centered as Java is. In java is there a way to make a base type variable that accepts all inherited subtypes? Like if I have;
class Mammal {...}
class Dog extends Mammal {...}
class Cat extends Mammal {...}
class ABC {
开发者_如何学编程 private Mammal x;
ABC() {
this.x = new Dog();
-or-
this.x = new Cat();
}
}
I need the variable to be able to accept any extended version too, but not in specific one extended kind.
There are some ways that I know, but don't want to use. I could make an attribute for each subtype, then only have the one attribute actually used. Make an array and shove it in there.
Any other ideas or a way to get a "base class" type variable?
Ok since I know using polymorphic duck typing isn't a great idea in Java, but since I don't think I can avoid it. Is the only way to use subclass methods dynamically to re assign a casted version of the varible ie, I get an error with this;
Mammal x;
x = new Dog();
System.out.println(x.getClass());
x.breath();
if (x instanceof Dog) {
x.bark();
} else if (x instanceof Cat) {
x.meow();
}
Saying symbol not found, however this works;
Mammal x;
x = new Dog();
System.out.println(x.getClass());
x.breath();
if (x instanceof Dog) {
Dog d = (Dog) x;
d.bark();
} else if (x instanceof Cat) {
Cat c = (Cat) x;
c.meow();
}
That last one the only way to do it?
If you have the following:
class Mammal {...}
class Dog extends Mammal {...}
class Cat extends Mammal {...}
Then Dog
is a subtype of Mammal
. Cat
is also a subtype of Mammal
. This type polymorphism does in fact allow you to do the following:
Mammal x;
x = new Dog(); // fine!
x = new Cat(); // also fine!
If in fact later there's the following:
class Platypus extends Mammal {...} // yes it's true!
Then you can also do:
x = new Platypus(); // fine!
This polymorphic subtyping relationship is one of the basic tenets of object-oriented programming.
See also
- Java Tutorials/Object-Oriented Programming Concepts
- Wikipedia/Polymorphism in object-oriented programming
Subtype polymorphism, almost universally called just polymorphism in the context of object-oriented programming, is the ability of one type, A, to appear as and be used like another type, B
On instanceof
type comparison operator
Suppose we have the following:
class Mammal { void lactate(); }
class Dog extends Mammal { void bark(); }
class Cat extends Mammal { void meow(); }
Then you can use instanceof
type comparison operator (§15.20.2) to do something like this:
Mammal x = ...;
if (x instanceof Dog) {
Dog d = (Dog) x;
d.bark();
} else if (x instanceof Cat) {
Cat c = (Cat) x;
c.meow();
}
if (x != null) {
x.lactate();
}
There are also ways to do this without if-else
; this is just given as a simple example.
Note that with appropriate design, you may be able to avoid some of these kinds of subtype differentiation logic. If Mammal
has a makeSomeNoise()
method, for example, you can simply call x.makeSomeNoise()
.
Related questions
- When should I use the Visitor Design Pattern? - sometimes used to simulate double dispatch
On reflection
If you must deal with new types not known at compile-time, then you can resort to reflection. Note that for general applications, there are almost always much better alternatives than reflection.
See also
- Java Technical Articles/Advanced Language Topics/Reflection
- Effective Java 2nd Edition, Item 53: Prefer interfaces to reflection
精彩评论