DEV Community

Saravanan B
Saravanan B

Posted on

Core Java - Polymorphism

Polymorphism Poly means many.
Morphic means Shapes.

It means a object can behave differently while communicating with different objects.

Two types of polymorphism
Compile time or static binding
Runtime or dynamic binding

Compile time which is also static binding we can achieve compile time polymorphism by method overloading.
Method overloading means same method name with different signature arguments or different number of arguments.


public class CompileTime {
    void add(int a, int b) {
        int result = a + b;
        System.out.println("Result is : " + result);
    }

    void add(float a, float b) {
        float result = a + b;
        System.out.println("Result is : " + result);
    }

    void add(int a, int b, int c) {
        int result = a + b + c;
        System.out.println("Result is : " + result);
    }

    public static void main(String[] args) {
        CompileTime ct = new CompileTime();
        ct.add(10, 20);
        ct.add(10.1f, 20.5f);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output :
Result is : 30
Result is : 30.6

Runtime Polymorphism

public interface Runtime {
        void start();
        void shutDown();
}
Enter fullscreen mode Exit fullscreen mode
public class MacBook implements Runtime {
    @Override
    public void start() {
        System.out.println("Inside macbook start");
    }
    @Override
    public void shutDown() {
        System.out.println("Inside macbook shutdown");
    }
}
Enter fullscreen mode Exit fullscreen mode
public class MacBookAir implements Runtime{
    @Override
    public void start() {
        System.out.println("Inside MacbookAir Start");
    }
    @Override
    public void shutDown() {
        System.out.println("Inside MacBookAir shut down");
    }
}
Enter fullscreen mode Exit fullscreen mode
public class RuntimePoly {
    public static void main(String[] args) {
            Runtime mac = new MacBook();
            Runtime air = new MacBookAir();
            mac.start();
            air.start();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
Inside macbook start
Inside MacbookAir Start

Object Casting
We can convert child class object to parent vise versa.

Upcasting is when we create a object for child from parent.
Runtime mac = new MacBook();

DownCasting is when we create a object for parent from child.
MacBook m1 = (MacBook)mac;

AutoTypeCasting -- The compiler will automatically promote to next available type if the exact type not found.

public class CompileTime {
    void add(float a) {
        System.out.println("Result is : " + a);
    }

    public static void main(String[] args) {
        CompileTime ct = new CompileTime();
        ct.add(10);
        ct.add(10.1f);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output - Result is : 10.0
Result is : 10.1

Overriding -- The static method in the parent cannot be override as non static methods. Vise versa non static method cannot be converted to static method in child.

If we use parent class to create a object for child and call the variable it will call the parent variable.

public interface Runtime {
        String s ="Hello";
}
Enter fullscreen mode Exit fullscreen mode
public class MacBook implements Runtime {
    String s = "Hello MacBook";
}
Enter fullscreen mode Exit fullscreen mode
public class RuntimePoly {
    public static void main(String[] args) {
                Runtime mac = new MacBook();
        System.out.println(mac.s);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output Hello

Overloading of main method.

public static void main(String[] args) {
        System.out.println("String[] args");
        main(10);
    }
    public static void main(int args) {
        System.out.println("int args overloaded main method");
    }
Enter fullscreen mode Exit fullscreen mode

Even though we overload main method JVM will take method with String[] args. We need to call the method with overloaded method in main method then only jvm will print the output of the method.

Top comments (0)