Untitled Document

Subscribe here to get updates on new Videos

 

16. Difference between Parent p = new Child(); and Child c = new Child(); ?

 

 

 

previous homenext

 

Difference between C c =new C(); and P p =new C()

C c= new C();
P p=new C()
1. If we know exact runtime type of object then we should use this approach. 1. if we don’t know exact runtime type of object then we should use this approach. (polymorphism)
2. By using child reference we can call both parent and child class methods. 2. By using parent reference we can call only methods available in parent class and child specific methods we cant call.
3. we can use child reference to hold only for that particular child class object only. 3. we can use parent reference to hold any child class object.