0
Java polymorphism
having class Animal and a Animal subclass Cat, what is the difference between declaring Animal a = new Cat() and Cat a = new Cat()?
2 Antworten
+ 1
1. a is Animal and you can not use Cat methods using a.catMethod(...). But it is possible to use ((Cat)a).catMethod(...).
2. a is Cat and you can use Cat methods.
0
When using Animal a = new Cat(); a is of type Animal and you can only use public methods and public variables that class Animal has. With Cat c = new Cat(); c is a type of Cat and you can use public methods + variables of Animal + Cat.