class Animal {
}
class Horse extends Animal {
}
public class Test {
public static void main(){
Animal a = new Horse();//mark1
}
}
Animal ah = new Horse();// reference type is Animal and object is Horse
I can’t fully understand this below:
Animal ah = new Horse();// reference type is Animal and object is Horse
I always keep the reference type the same with the object
I want to know the reason of using the not-same reference type.Please tell me some condition that it’s more appropriate to use it
this works for several occasions, a simple example would be if you needed an array of animals:
You could have some defined generic methods or have them specified in for each class that extends.
While in Animal you could not know the thing to do, so you could use somthing like
The thing is that if you go throug every animal in your array it would use the method defined for it.
would output: