INTRODUCTION:
Helooooo folks.. In this tutorial I am going to explain
about method overriding and difference between method overloading and method
overriding
WHAT METHOD OVERRRIDING ACTUALLY IS..?
Method overriding
actually comes into life when the inheritance concepts comes into being..!! If
you don’t know what inheritance actually mean ..please refer here for the
detailed post Inheritance in java
WHAT DOES A METHOD ACTUALLY RIDES..?
When a sub class
actually contain same return type, same parameters, same name etc then that method is said to override
the SUPER CLASS method..
Ok ..I don’t want to confuse more with technical terms..let
me explain with an example..
Class Dad
{
String color = “white”;
String haricolor= “black”;
public void Smartness( )
{
System.out.println(“ I am dad and I am smart ;) “);
}
public void Diabetes( )
{
System.out.println(“ Ooops.. I am dad and I have diabetes”
);
}
}
Class Son extends Dad
{
String Height = “tall”;
public void Smartness( )
{
System.out.println(“ I am dad and I am smart ;) “);
}
public void swimming ( )
{
System.out.printlln(“Hieeee..I am the son and I know
swimming “);
}
public static void main(String args[])
{
Son Sonobj = new Son ( ) ;
Sonobj.swimming( );
Sonobj.Smartness( );
}
}
OUTPUT:
Hieeee..I am the son and I know swimming
I am SON and I am smart ;)
EXPLANATION:
Here dad is the Super class and Son is the sub class.. You
can see.. the sub class SON is having the same method as the super class and when we call the Smartness method then it
calls the method in the Son class and not the Dad class.
This is because
“SMARTNESS method has overridden the method in the super class” and so this
method is called.. !!
But what if we want to call the SMARTNESS method in the
SUPER CLASS ?
IS THERE ANY WAY TO CALL THE SMARTNESS METHOD IN SUPER
CLASS??
Of course it’s a big ssssss !! We can call the method in the
super class using the keyword “SUPER”
Wow.. its really SUPER.. !! nah ;)
HOW TO USE SUPER KEYWORD..?
Considering the same example as above.. but extra only one
keyword is added.. Look at the Smartness
method in the Son class..
Class Dad
{
String color = “white”;
String haricolor= “black”;
public void Smartness( )
{
System.out.println(“ I am dad and I am smart ;) “);
}
public void Diabetes( )
{
System.out.println(“ Ooops.. I am dad and I have diabetes”
);
}
}
Class Son extends Dad
{
String Height = “tall”;
public void Smartness( )
{
System.out.println(“ I am SON and I am smart ;) “);
Super.Smartness( );
}
public void swimming ( )
{
System.out.printlln(“Hieeee..I am the son and I know
swimming “);
}
public static void main(String args[])
{
Son Sonobj = new Son ( ) ;
Sonobj.swimming( );
Sonobj.Smartness( );
}
}
OUTPUT:
Hieeee..I am the son and I know swimming
I am SON and I am smart ;)
I am dad and I am smart ;)
EXPLANATION:
Look at the example above..by using “SUPER.SMARTNESS( ) “ ..
the smartness( ) method of the DAD class is called and printed.
So only way of accessing the super class method if the
method is overridden ie. if the same method is present in the sub class with
the same type .. etc then it can be done using SUPER KEYWORD.
CONCLUSION:
So that’s it for this post. Hope you understood the concepts
better.. !! Hit like button ..share the post..drop your comments below..It will
help me to post more !! Suggestions and
request for post you wish is welcomed.. Have a peaceful day :)