What is difference between Inheritance and Method Overriding?? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 6

What is difference between Inheritance and Method Overriding??

Give me a simple example

21st Jan 2021, 7:23 AM
K.Suhanya
K.Suhanya - avatar
22 Respuestas
+ 10
Inheritance enable us to define a class that takes all the functionality from parent class and allows us to add more.  Method overriding occurs simply defining in the child class a method with the same name of a method in the parent class .
21st Jan 2021, 7:30 AM
RKK
RKK - avatar
+ 6
Page-1 Difference between inheritance and method overriding : Suppose your own family has this rule of eating foods at a fix timing, let say class Family{ // family food timing function food_timing() { breakfast at 8 am; lunch at 1 pm; dinner at 7 pm; } } Suppose you got married to someone and now you have your spouse family and lets say you want to "INHERIT" all your own family's food timing in your Spouse family. So we need to do this class SpouseFamily extends Family{ // this is inherited from Parent class Family function food_timing() { breakfast at 8 am; lunch at 1 pm; dinner at 7 pm; } } contd.
21st Jan 2021, 9:11 AM
RKK
RKK - avatar
+ 6
Page-2 But now the problem is that you and your spouse don't like the dinner timing, so you guys want to change it to 9 pm instead of 7 pm, now you will need this concept of "METHOD OVERRIDING" here class SpouseFamily extends Family{ // this is inherited from Parent class Family function food_timing() { breakfast at 8 am; lunch at 1 pm; dinner at 7 pm; } // method overriding happens here function food_timing() { breakfast at 8 am; lunch at 1 pm; dinner at 9 pm; } } function main() { // output the timing of your own family Family o1 = new Family(); print(o1.food_timing()); // output the timing of your Spouse family Family o2 = new SpouseFamily(); print(o2.food_timing()); } contd.
21st Jan 2021, 9:12 AM
RKK
RKK - avatar
+ 5
Method overriding is useful when you extend a class, lets say you have a class called Dog and a class called Cat which extends Dog. Dog class has a method called makeSound() which prints "Bark"; if we create an object of Cat class and call makeSound() we get "Bark" Well Cats dont Bark they Meow so to change this we go into the cat class and override the inherited makeSound() method from Dog and make re-write the method in Cat class and its data to print "meow!. When we call MakeSound() method again on our cat object in main method we get "meow!" This is because at runtime java can see that makeSound() is being called on a Cat object and the overridden method is called instead.
21st Jan 2021, 11:55 AM
D_Stark
D_Stark - avatar
+ 4
Page-3 But remember there are rules for overriding method too, so you will need to check them from the internet. (I AM SORRY FOR THE EXAMPLE TOO) it was meant to only make the concept clear.
21st Jan 2021, 9:17 AM
RKK
RKK - avatar
+ 4
Thanks D_Stark
21st Jan 2021, 12:05 PM
K.Suhanya
K.Suhanya - avatar
+ 4
Neha Singh method overriding
22nd Jan 2021, 9:47 AM
K.Suhanya
K.Suhanya - avatar
+ 4
Rajbali Turha ūüėē
23rd Jan 2021, 4:01 AM
K.Suhanya
K.Suhanya - avatar
+ 3
Atul sorry... there is no clearance
21st Jan 2021, 7:44 AM
K.Suhanya
K.Suhanya - avatar
+ 3
RKK... I couldn't understand
21st Jan 2021, 7:45 AM
K.Suhanya
K.Suhanya - avatar
+ 3
D_Stark in your example does speak() call MakeSound()?
21st Jan 2021, 7:18 PM
Sonic
Sonic - avatar
+ 2
Rkk provided a good explanation, When you extend base class in child class then that is inheritance and you will be able to use base class method and properties but if you provide a different implementation in child class for a method used in base class then that's method overidding
21st Jan 2021, 8:07 AM
Abhay
Abhay - avatar
+ 1
Here.... When you see a coding, how do you identify that is inheritance or overriding???
21st Jan 2021, 7:48 AM
K.Suhanya
K.Suhanya - avatar
+ 1
Kanesakumar Suhanya all these method go into a Vtable if you research an image this it will give you a better understanding what's happening behind the scenes.
21st Jan 2021, 1:13 PM
D_Stark
D_Stark - avatar
+ 1
Method overriding Or overloading
22nd Jan 2021, 9:37 AM
Neha Singh
Neha Singh - avatar
+ 1
Sonic nice one bro, I have amended thanks ūüėČ
22nd Jan 2021, 9:51 AM
D_Stark
D_Stark - avatar
0
Kanesakumar Suhanya Sorry for that
21st Jan 2021, 8:11 AM
Atul [Inactive]
0
Both are the different concepts of oops one is inheritance second is polymorphism
22nd Jan 2021, 5:22 PM
Amber Tripathi
Amber Tripathi - avatar
0
Hii
23rd Jan 2021, 3:40 AM
Rajbali Turha
Rajbali Turha - avatar