logo
Java Polymorphism
Polymorphism
Polymorphism came from the two Greek words poly means many and morphos means forms. The ability to exist in different forms is called polymorphism. In Java, a variable, an object or a method can exist in different forms, thus performing various tasks depending on the context. Because same variable or method can perform different tasks, the programmer has the advantage of writing flexible code.
Java Images
Method Overloading :
1) Two methods are said to be overloaded methods if and only if two methods are having same name but different argument list. 

2) We can overload the methods in two ways in java language
a. Provide the different number of arguments to the same methods. 
b. Provide the same number of arguments with different data types. 

3) If we want achieve overloading one class is enough. 

4) It is possible to overload any number of methods. 

Types of overloading :
a. Method overloading
b. Constructor overloading
c. Operator overloading implicitly by the JVM(addition& concatenation)
Method overloading
class Test 
{ 
void m1(char ch) 
{ 
System.out.println(" char-arg method"); 
} 
void m1(int i) 
{
System.out.println("int-arg method"); 
} 
void m1(int i,int j) 
{ 
System.out.println(i+j); 
} 
public static void main(String[] args) 
{ 
Test t=new Test(); 
t.m1('a'); 
t.m1(10); 
t.m1(10,20); 
} 
}
Output :
output: char-arg method,,nt-arg method,,30
main method Overloading
class sup 
{ 
public static void main(String[] args) 
{ 
System.out.println("String[] parameter main method start"); 
main(100); 
main('r'); 
} 
public static void main(int a) 
{ 
System.out.println("integer parameter argument"); 
System.out.println(a); 
} 
public static void main(char ch) 
{ 
System.out.println("character paramer argument"); 
System.out.println(ch); 
} 
}
Output :
output:String[] parameter main method start,, integer parameter argument,, 100,, character paramer argument,, r
Constructor Overloading
class Test 
{ 
Test() 
{ 
System.out.println("hi"); 
} 
Test(int i) 
{
System.out.println(i); 
} 
Test(int ch,int i) 
{ 
System.out.println(ch+i); 
} 
public static void main(String[] args) 
{ 
Test t1=new Test();//calling of zero argument constructor 
Test t2=new Test(10);//calling of one argument constructor 
Test t3=new Test(20,100);//calling of two argument constructor 
} 
}
Output :
output:hi,,10,120
Operator overloading
class Test 
{ 
public static void main(String[] args) 
{ 
System.out.println("str1"+"str2"); 
System.out.println(10+20); 
} 
} 
Output :
output:str1str2,,30
Method Overriding:-
1) If the child class not satisfy the parent class method implementation then it is possible to override that method in the child class based on child class requirement.
2) If we want to achieve method overriding we need two class(child and parent).
3) In the overriding concept the child class and parent class method signatures must be same otherwise we are getting compilation error.
The parent class method is called------------- overridden method
The child class method is called---------------overriding method
overriding method
class Teacher
{ 

void marks() 
{ 
System.out.println("100");  //Overridden method 
} 
}; 
class student extends Teacher
{ 
void marks() 
{ 
System.out.println("just pass"); //overriding method 
} 
public static void main(String[] args)
{ 
student c=new student (); 
c.marry(); 

}};
Output :
output:just pass