Method Overloading:-
Two methods are said to be overloaded method if and only if, both methods having the same name but different arguments types.
In the old language like C method overloading concept is not available hence, we can't declare multiple methods with same name but different arguments types.
But in Java, we can declare multiple methods with same name but different arguments types.Such type of methods is called the overloaded method. This overloading feature of java reduces the complexity of programming.
example:
class Test
{
public void m1( )
{
System.out.println( "no - arguments m1() method");
}
public void m1( int x)
{
System.out.println( "integer - arguments m1() method");
}
public void m1( double y)
{
System.out.println( "double - arguments m1() method");
}
public static void main( String [ ] args)
{
Test t = new Test( );
t.m1( );
t.m1(20 );
t.m1(20.5);
}
}
Output is:
no -arguments m1() method
integer - arguments m1() method
double -arguments m1() method
In Overloading method resolution is always takes care by the compiler based on the reference type hence overloading is also known as compile-time polymorphism or static polymorphism.
example:-
class Year
{
}
class Semester extends Year
{
}
class Test
{
public void m1(Year y)
{
System.out.println( "Year class");
}
public void m1(Semester s)
{
System.out.println( "Semester class ");
}
public static void main(String args[ ])
{
Test t = new Test( );
Year y = new Year( );
t.m1(y); ...............(1)
Semester s = new Semester( );
t.m1(s); ..................(2)
Year y = new Semester( );
t.m1( y); ...............(3)
}
}
Output of ......(1)
Year class
Output of ......(2)
Semester class
Output of ......(3)
Year class
Important Loop Holes In Method Overloading
Case 1. Automatic promotion in overloading:-
While resolving overloaded method if exact match method is not available then we won't get any compile time error immediately.
First, it will promote argument to the next level and check whether the matched method is available or not. If matched method is available it will be considered.
If the matched method is not available then compiler promotes arguments once again to the next level, this process will be continued until all possible promotion.
Still, if the matched method is not available then finally we will get compile - time error.
These following are the all possible promotions in overloading. Overloading
example:-
class Test
{
public void m1( int x)
{
System.out.println( "integer - arguments m1() method");
}
public void m1( float y)
{
System.out.println( "float - arguments m1() method");
}
public static void main( String [ ] args)
{
Test t = new Test( );
t.m1(10 );
t.m1(10.5f );
t.m1('a');
t.m1(10L);
}
}
Output is:
integer - arguments m1() method
float -arguments m1() method
integer - arguments m1() method
float - arguments m1() method
Case 2. Precedence of the child class is high compared to parent class:-
While resolving overloaded methods compiler will always give the precedence for child type argument then compared with parent type argument.
example:-
class Test
{
public void m1(String s)
{
System.out.println("String Version");
}
public void m1(Object o)
{
System.out.println("Object Version");
}
public static void main(String args [ ])
{
Test t = new Test( );
t.m1("javabychetan")
t.m1( new Object( ));
t.m1( null);
}
}
Output is:-
String Version
Object Version
String Version
Case 3. The normal method gets High Priority than var-arg Method:-
In general var-arg method gets the least priority.
If no other method matched then the var-arg method will be executed.
example:-
class Test
{
public void m1(int i)
{
System.out.println("general method called ");
}
public void m1(int... i)
{
System.out.println("var-arg method called ");
}
public static void main(String args [ ])
{
Test t = new Test( );
t.m1( )
t.m1( 10,20);
t.m1( 10);
}
}
Output is:-
var-arg method called
var-arg method called
general method called
No comments:
Post a Comment
Be the first to comment!
Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.
Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
Regards,
JavaByChetan
Back To Home