What is different between ...... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What is different between ......

Between Animal a = new Dog(); And Dog a = new Dog(); Animal is base class

6th Apr 2020, 2:02 PM
Mohammad
Mohammad - avatar
3 Answers
+ 4
Mohammad in the second case you have the access to the methods and variables of "Animal" class as well "Dog" class. Whereas, in the first case you only have access to the methods and variables of "Animal" class.
6th Apr 2020, 2:11 PM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
+ 2
I got it Thanks
6th Apr 2020, 2:12 PM
Mohammad
Mohammad - avatar
+ 1
1) You have an Animal type reference to point to a Dog type object. Only Animal class methods will be invoked if you use method overloading and only Dog class methods will be invoked if you use method overriding in your code. 2) You have a Dog type reference to point to a Dog type object. All the methods in the Dog class can be invoked through reference 'a'.
6th Apr 2020, 2:15 PM
Avinesh
Avinesh - avatar