I’m preparing to SCJP and looks like I don’t understand class cast principles.
class Dog extends Animal
First way of creating Dog() instance- make instance of Animal and cast it to Dog(upcast):
Animal a = new Dog();
Dog d1 = (Dog)a;
VS
Second way of creating Dog() instance – make it directly:
Dog d2 = new Dog();
what is the difference between d1 and d2 objects this case?
d1andd2are just reference to dog, both will eventually refer to an instance of Dog .there is no difference
Also See