Opening Hours :7AM to 9PM
When ever same behaviour is existing multiple times with separate implementation is known as PolyMorphism.
The main advantage with PolyMorphism is reusability.(both code reusability and object reusability).
because of code reusability burden of the developer will be reduced and code readability will be increased.
as per as java language is concent if the same method name existing multiple times with different signature with different implementation is known as PolyMorphism.
In java language these PolyMorphisms are classified into following types
1. Static PolyMorphism
2. Dynamic PolyMorphism
Whenever method call binds with method body at the time of compilation of the program is known as static PolyMorphism or compile time PolyMorphism or early or static binding.
Binding represents establishing relation between method call and method body.
class Demo { static void show() { ... -- method body ... } } public class Main { public static void main(String args[]) { Demo.show(); -- method call } }
whenever method call binds with method body at the time of running of the program is known as dynamic PolyMorphism or run time PolyMorphism or late or dynamic binding.
Binding represents establishing relation between method call and method body.
class Demo { void show() { ... -- method body ... } } public class Main { public static void main(String args[]) { Demo d=new Demo(); d.show(); -- method call } }
in the above example method call binds with method body at runtime.Why because these methods called with object reference.Object will be handled by JVM at Run time.
class Demo { void show() { System.out.println("hi"); } void show() { System.out.println("welcome"); } void show() { System.out.println("java"); } } public class Main { public static void main(String args[]) { Demo d=new Demo(); d.show(); } }
These are classified into two types.
1.Method Overloading
2.Constructor Overloading
Whenver same method name is existing multiple times with different implementation(number of parameters,order of parameters,type of parameters) in the same class(Static or Dynamic PolyMorphism)
Number of parameters syntax:
class Demo
{
void show()
{
System.out.println("Hi");
}
void show(int x)
{
System.out.println("Welcome");
}
}
Order of parameters syntax:
class Demo
{
void show(int x,float y)
{
System.out.println("Hi");
}
void show(float x,int y)
{
System.out.println("Welcome");
}
}
Type of parameters syntax:
class Demo
{
void show(int x)
{
System.out.println("Hi");
}
void show(float y)
{
System.out.println("Welcome");
}
}
class Demo { static void show() { System.out.println("hi"); } static void show(int x) { System.out.println("welcome"); } static void show(int x,int y) { System.out.println("java"); } } public class Main { public static void main(String args[]) { Demo.show(); Demo.show(10); Demo.show(10,20); } }
class Demo { void show() { System.out.println("hi"); } void show(int x) { System.out.println("welcome"); } void show(int x,int y) { System.out.println("java"); } } public class Main { public static void main(String args[]) { Demo d=new Demo(); d.show(); d.show(10); d.show(10,20); } }
Whenver same Constructor name is existing multiple times with different implementation(number of parameters,order of parameters,type of parameters) in the same class.
Number of parameters syntax:
class Demo
{
Demo()
{
System.out.println("Hi");
}
Demo(int x)
{
System.out.println("welcome");
}
}
Order of parameters syntax:
class Demo
{
Demo(int x,float y)
{
System.out.println("Hi");
}
Demo(float x,int y)
{
System.out.println("welcome");
}
}
Type of parameters syntax:
class Demo
{
Demo(int x)
{
System.out.println("Hi");
}
Demo(float y)
{
System.out.println("welcome");
}
}
class Demo { Demo() { System.out.println("hi"); } Demo(int x) { System.out.println("welcome"); } Demo(int x,int y) { System.out.println("java"); } } public class Main { public static void main(String args[]) { Demo d=new Demo(); Demo d=new Demo(10); Demo d=new Demo(10,20); } }
These are classified into two types.
1.Method Overridding
2.Constructor Overridding
Whenver same method name is existing multiple times with different implementation(number of parameters,order of parameters,type of parameters) in the parent and child classes(Static or Dynamic PolyMorphism)
Number of parameters syntax:
class Demo
{
void show()
{
System.out.println("Hi");
}
}
class Demo1 extends Demo
{
void show(int x)
{
System.out.println("welcome");
}
}
Order of parameters syntax:
class Demo
{
void show(int x,float y)
{
System.out.println("Hi");
}
}
class Demo1 extends Demo
{
void show(float x,int y)
{
System.out.println("welcome");
}
}
Type of parameters syntax:
class Demo
{
void show(float x)
{
System.out.println("Hi");
}
}
class Demo1 extends Demo
{
void show(int x)
{
System.out.println("welcome");
}
}
class Demo { static void show() { System.out.println("hi"); } } class Demo1 extends Demo { static void show(int x) { System.out.println("welcome"); } } public class Main { public static void main(String args[]) { Demo1.show(); Demo1.show(10); } }
class Demo { void show() { System.out.println("hi"); } } class Demo1 extends Demo { void show(int x) { System.out.println("welcome"); } } public class Main { public static void main(String args[]) { Demo1 d=new Demo1(); d.show(); d.show(10); } }
Whenver same Constructor name is existing multiple times with different implementation(number of parameters,order of parameters,type of parameters) in the parent and child class.
Constructor overridding not possible because constructors can not inherit from parent class to child class.