+ 6
What is difference between Inheritance and Method Overriding??
Give me a simple example
22 odpowiedzi
+ 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 .
+ 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.
+ 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.
+ 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.
+ 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.
+ 4
Thanks D_Stark
+ 4
Neha Singh method overriding
+ 4
+ 3
Atul sorry... there is no clearance
+ 3
RKK... I couldn't understand
+ 3
D_Stark in your example does speak() call MakeSound()?
+ 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
+ 1
Here....
When you see a coding, how do you identify that is inheritance or overriding???
+ 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.
+ 1
Method overriding Or overloading
+ 1
Sonic nice one bro, I have amended thanks 😉
0
Kanesakumar Suhanya Sorry for that
0
Both are the different concepts of oops one is inheritance second is polymorphism
0
Hii