• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

PolyMorphism


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

Static 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
}
}
class Main
{
public static void main(String args[])
{
Demo.show(); -- method call
}
}

Dynamic PolyMorphism:
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
}
}
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.

PolyMorphism Program



class Demo
{
void show()
{
System.out.println("hi");
}
void show()
{
System.out.println("welcome");
}
void show()
{
System.out.println("java");
}
}
class Main
{
public static void main(String args[])
{
Demo d=new Demo();
d.show();
}
}
Error: method show already existing in the Demo class.
Note:
But Here PolyMorphism means same method name existing multiple times.But here if you take same method name multiple error will displayed.To overcome this problems overloading and overridding are introduced.


Output:
method show already existing in the Demo class.
Overloading:
These are classified into two types.
1.Method Overloading
2.Constructor Overloading
Method 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)

Syntax:
// Number of parameters
class Demo
{
void show()
{
System.out.println("Hi");
}
void show(int x)
{
System.out.println("Hi");
}
}

// Order of parameters
class Demo
{
void show(int x,float y)
{
System.out.println("Hi");
}
void show(float x,int y)
{
System.out.println("Hi");
}
}

// Type of parameters

class Demo
{
void show(int x)
{
System.out.println("Hi");
}
void show(float y)
{
System.out.println("Hi");
}
}

Static PolyMorphism
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");
}
}
class Main
{
public static void main(String args[])
{
Demo.show();
Demo.show(10);
Demo.show(10,20);
}
}

Output:
hi
welcome
java
Dynamic PolyMorphism
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");
}
}
class Main
{
public static void main(String args[])
{
Demo d=new Demo();
d.show();
d.show(10);
d.show(10,20);
}
}

Output:
hi
welcome
java
Constructor Overloading:
Whenver same Constructor name is existing multiple times with different implementation(number of parameters,order of parameters,type of parameters) in the same class.

Syntax:
// Number of parameters
class Demo
{
Demo()
{
System.out.println("Hi");
}
Demo(int x)
{
System.out.println("Hi");
}
}

// Order of parameters
class Demo
{
Demo(int x,float y)
{
System.out.println("Hi");
}
Demo(float x,int y)
{
System.out.println("Hi");
}
}

// Type of parameters
class Demo
{
Demo(int x)
{
System.out.println("Hi");
}
Demo(float y)
{
System.out.println("Hi");
}
}

class Demo
{
Demo()
{
System.out.println("hi");
}

Demo(int x)
{
System.out.println("welcome");
}

Demo(int x,int y)
{
System.out.println("java");
}
}
class Main
{
public static void main(String args[])
{
Demo d=new Demo();
Demo d=new Demo(10);
Demo d=new Demo(10,20);
}
}


Output:
hi
welcome
java

Overridding:
These are classified into two types.
1.Method Overridding
2.Constructor Overridding

Method 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)
Syntax:
// Number of parameters
class Demo
{
void show()
{
System.out.println("Hi");
}
}
class Demo1 extends Demo
{
void show(int x)
{
System.out.println("welcome");
}
}

Static PolyMorphism
class Demo
{
static void show()
{
System.out.println("hi");
}
}
class Demo1 extends Demo
{
static void show(int x)
{
System.out.println("welcome");
}
}
class Main
{
public static void main(String args[])
{
Demo1.show();
Demo1.show(10);
}
}


Output:
hi
welcome

Dynamic PolyMorphism
class Demo
{
void show()
{
System.out.println("hi");
}
}
class Demo1 extends Demo
{
void show(int x)
{
System.out.println("welcome");
}
}
class Main
{
public static void main(String args[])
{
Demo1 d=new Demo1();
d.show();
d.show(10);
}
}

Output:
hi
welcome
Overridding:
Constructor Overridding:

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.

Note:
Constructor overridding not possible because constructors can not inherit from parent class to child class.