Polimorphism

When ever same behavoiur is existing multiple times with separate implementation is known as polimorphism.
The main advantage with polimorphism 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 polimorphism.
In java language these polimorphisms are classified into following types
  • Static polimorphism
  • Dynamic polimorphism
  • Static polimorphism:
  • whenever method call binds with method body at the time of compilation of the program is known as static polimorphism or compile time polimorphism 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 polimorphism:
  • whenever method call binds with method body at the time of running of the program is known as dynamic polimorphism or run time polimorphism 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.

Polimorphism 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();
}
}
If you compile this program error will will be displayed.
Error:
method show already existing in the Demo class.

Note: But Here Polimorphism 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.

Overloading:

These are classified into two types.
  • Method Overloading
  • Constructer Overloading
  • Method Overloading:
  • Whenver same behaviour is existing multiple times with different implementation(number of parameters,order of parameters,type of parameters) in the same class(Static or Dynamic polimorphism)
    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 Polimorphism

    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(); } }
        

    Dynamic Polimorphism

    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(); } }
  • Constructer Overloading:
  • Whenver same constructer 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();
    }
    }
    

Overridding:

These are classified into two types.
  • Method Overridding
  • Constructer Overridding
  • Method Overridding:
  • Whenver same behaviour 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 polimorphism)
    syntax:
    // Number of parameters        
    class Demo
    {
    void show()
    {
    System.out.println("Hi")
    }
    }
    class Demo1 extends Demo
    {
    void show(int x)
    {
    System.out.println("Hi")
    }
    } 
    
        

    Static Polimorphism

    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(); } }
        

    Dynamic Polimorphism

    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(); } }
  • Constructer Overridding:
  • Whenver same constructer name is existing multiple times with different implementation(number of parameters,order of parameters,type of parameters) in the parent and child class.
    Note: Constructer overridding not possible because constructors can not inherit parent class child class.