Java Polymorphism

Polymorphism is ability to take more than one form. That means name are same but it can be perform different different task.

Compile time Polymorphism

Function overloading is example of compile time polymorphism. In this situation function name are same but their parameter type are different or in different sequence. This parameter type is decide which function are executes.

//Compile time Polymorphism
public class Overloading{
    //Function test this are accept one integer values
    void test(int x){
        System.out.println("Fist test Function Call");
        System.out.println("X : "+x);
    }
    //Function test this are accept two integer values
    void test(int x,int y){
        System.out.println("Second test Function Call");
        System.out.println("X : "+x+ " Y : "+y);
    }
    //Function test this are accept two double type values
    double test(double x,double y){
        System.out.println("Third test Function Call");
        //Return a double value
        return x+y;
    }
    public static void main(String[] args) {
        //Craete object of class
        Overloading obj = new Overloading();
        //Executed function which are accept one integer parameter      
        obj.test(10);
        
        //Execute function which are accept two double type parameter
        double result=obj.test(10.3,10.5);


        System.out.println("Result : "+result);

        //Execute function which are accept two integer parameter
        obj.test(9,4);

    }
}

Output

Fist test Function Call
X : 10
Third test Function Call
Result : 20.8
Second test Function Call
X : 9 Y : 4

Function overloading is based on function parameter list argument not based on it's return type. Resolving the ambiguity of function execution process at compile time that process is called compile time polymorphism. There are also know as static polymorphism. See other example.

//Compile time Polymorphism
public class Overloading{

    private int x,y;
    Overloading(int x,int y){
        this.x=x;
        this.y=y;
    }
    void test(){
        System.out.println("Test Function Overloading");
        //Execute another test functions
        test(x+10);
        
    }
    //Function test this are accept one integer values
    void test(int x){
        System.out.println("\nCheck One parameter Data");
        System.out.println("Local x : "+x+ " Member x :"+this.x);
        test(x+4,y+10); //local x and member y
    }
    //Function test this are accept two integer values
    void test(int x,int y){
        System.out.println("\nCheck Two parameter Data");
        System.out.println("Local x : "+x+ " Member x :"+this.x);
        System.out.println("Local y : "+y+ " Member y :"+this.y);
    }
   
    public static void main(String[] args) {
        //Craete object of class
        Overloading obj = new Overloading(2,6);
        //Executed parameter-less function      
        obj.test();
    }
}
Compile time Polymorphism

Output

Test Function Overloading

Check One parameter Data
Local x : 12 Member x :2

Check Two parameter Data
Local x : 16 Member x :2
Local y : 16 Member y :6

Runtime Polymorphism

method Override are achieve runtime polymorphism in java programming.

//Runtime Polymorphism Example
class A {

    void reflection(){

        System.out.println(" Class A reflection Method Work");
    }
}

class B extends A{
    @Override
    void reflection(){

        System.out.println(" Class B reflection Method Work");
    }
}

class C extends B{
    @Override
    void reflection(){

        System.out.println(" Class C reflection Method Work");
    }

}
class Overloading{

    public static void main(String[] args) {
        A objTest; //Variables

        //Create instance of class B
        objTest = new  B();
        objTest.reflection(); //access class B method

        //Create instance of class A
        objTest = new A();
        objTest.reflection(); //access class A method

        //Create instance of class C
        objTest = new C();
        objTest.reflection(); //access class C method
    }
}

Output

 Class B reflection Method Work
 Class A reflection Method Work
 Class C reflection Method Work

Other example

//Runtime Polymorphism Example
class A {
    private int x,y;
    public A(int x,int y){
        this.x=x;
        this.y=y;
    }
    void reflection(){

        System.out.println("\nClass A reflection Method Work");
        System.out.println("Here x : "+x+ " \t y : "+y);
        System.out.println("("+ x+ " X "+y+")  = "+(x-y));
    }
}

class B extends A{

    private float x,y;
    public B(float x, float y ){
        super(4,7);
        this.x=x;
        this.y=y;
    }

    @Override
    void reflection(){

        System.out.println("\nClass B reflection Method Work");
        System.out.println("Here x : "+x+ " \t y : "+y);
        System.out.println("("+ x+ " - "+y+")  = "+(x-y));
    }
}

class C extends B{
    
    private double x,y;
    public C(double x, double y ){
        super(5.3f,2.5f);
        this.x=x;
        this.y=y;
    }
    @Override
    void reflection(){

        System.out.println("\nClass C reflection Method Work");
        System.out.println("Here x : "+x+ " \t y : "+y);
        System.out.println("("+ x+ " + "+y+")  = "+(x-y));
    }

}
class Overloading{

    public static void main(String[] args) {
        A objTest; //Variables

        //Create instance of class A
        A objOne = new A(2,7);

        //Create instance of class B
        B objTwo = new  B(3.04f,4.06f);
       
        //Create instance of class C
        C objThree = new C(5.1,2.1);
        
        //Assigning reference of an instance
        objTest=objTwo; //class B instance
        objTest.reflection(); //access class B method

        objTest=objThree;
        objTest.reflection(); //access class C method

        objTest=objOne;
        objTest.reflection(); //access class A method
    }
}
Runtime Polymorphism

Output

 Class B reflection Method Work
Here x : 3.04    y : 4.06
(3.04 - 4.06)  = -1.02

Class C reflection Method Work
Here x : 5.1     y : 2.1
(5.1 + 2.1)  = 2.9999999999999996

Class A reflection Method Work
Here x : 2   y : 7
(2 X 7)  = -5


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved