+ 1

Inheritance and method overriding

Can anybody please explain this question with the output. https://www.sololearn.com/post/180153/?ref=app

22nd Nov 2019, 6:17 PM
Avinesh
Avinesh - avatar
8 ответов
+ 5
Avinesh review this code you will get to know what is happening https://code.sololearn.com/cc35W1B8fs3s/?ref=app
22nd Nov 2019, 6:49 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 5
Avinesh yeah somehow the right wording can be Is-A relationship between the classed so whenever we invoked overriding function that is worked like run time polymorphic function and return that value which class object is invoked and overriden this override leads to the code snippet gives output like this way IS-A relationship is mentaining in the child parent and grandparent class at run time when overriden function are invoked this link will give you some more information about this https://www.javatpoint.com/method-overriding-in-java
22nd Nov 2019, 6:59 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 1
Sami Khan Kindly check the post. I am aware of what you mentioned, but still thanks for that explanation.
22nd Nov 2019, 6:26 PM
Avinesh
Avinesh - avatar
+ 1
GAWEN STEASY so only the reference changes but the instance remain of the same type. And overridden methods are invoked based on the instance and not of the reference. Did I say it right?
22nd Nov 2019, 6:53 PM
Avinesh
Avinesh - avatar
+ 1
Try to add some more methods like in DerivedB class.. say demob(){ sopln("demob"); } Now if you try to call this method using b1 reference variable.. you will get an error, because the reference is type of base.. and it bounds to call only base type methods.
22nd Nov 2019, 7:05 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
0
Inheritance allows to inherit the properties from base class to child class.. Method overriding is a type of runtime polymorphism, which depends on inheritance.. to override a method there must be inheritance.. And in method overriding we create reference type of parent class but it refers to the object of child class.. Ex: Parent obj = new Child(); This achieves runtime polymorphism.. But in inheritance: We create reference and object both of child class.. which takes the property of the parent class and we can call parent method as well.. Child obj = new Child();
22nd Nov 2019, 6:25 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
0
Oh sorry.. let me see..
22nd Nov 2019, 6:36 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
0
Sami Khan Thank you. GAWEN STEASY I really appreciate the effort you put in to help me understand this. Thanks a lot.
22nd Nov 2019, 7:03 PM
Avinesh
Avinesh - avatar