Hello everyone! What's up? Are you ready to go with Java? This is another OOP aricle..The 3rd concept..Polymorphism.. We can not forget these concept when we learn programming with Java. Without more explanations, I 'll go forward..

What is Polymorphism?

This is derived by two Greek words. They are Poly and morphs. In Greek, Poly means many and morphs means forms. So when we combine them, it becomes many forms. In java another word phrase is added to this and says Single Interface-Many Forms. When we apply this to OOP, Polymorphism means working with objects of various types and classes through a single, uniform interface. Simply, different functionalities are achieved using one method call. Let me describe...

Mainly Java has TWO types of Polymorphism. They are ;

1. Compile Time Polymorphism / Static Binding
2. Run Time Polymorphism / Dynamic Binding

Compile Time Polymorphism

In Java, Compile Time Polymorphism is achieved and explained through Method Overloading. This means, having several methods in a class with the same name but different signatures. Different signatures means the methods have different parameter lists. Here, Java compiler knows the method to be invoked, checking its signatures. I will explain this using an example.
Check the below example...
class AddNumbers {

    // method with 2 integer parameters
    public int addition(int x, int y){
        return x+y;
    }
    // method with 3 integer parameters
    public int addition(int x, int y, int z){
        return x+y+z;
    }
    // method with 2 integer and double parameters
    public int addition(double x, int y){
        return (int)x+y;    //casting
    }
}

class Mathematics{
    public static void main(String args[]){
        AddNumbers a1 = new AddNumbers();
        System.out.println(a1.addition(1,2));
        System.out.println(a1.addition(1,2, 3));
        System.out.println(a1.addition(3,4));
    }
}
Output:

Here you can see all the 3 methods have the same name. But no errors..If you remove the parameter z from method 2, then there will be error due to have the same signature. This in method overloading and also Compile Time Polymorphism.


Run Time Polymorphism

This is the second type. How to achieve this? Usually Java explains this concept using Method Overriding. The technique we use called Dynamic Method Dispatch.What does this process do?
Here, a method existing in a Super class is overridden by its Sub class. That overridden method is called using a Super class Reference variable. Sub class has a method with the same name and same signature that its Super class has. During the process, Dynamic method dispatch occurs. It means, Java Compiler tries to execute a method called through a Super class reference variable. But there's a method with same name and signature in the Sub class. So, immediately, the newer version of the method within the Sub class is executed by JVM(Java Virtual Machine).

class Vehicle {
    // super class method 1
    public void park(){
        System.out.println("Vehicle parks");
    }
    // super class method 2
    public void drive(){
        System.out.println("Vehicle is driven by a man");
    }
}

class Car extends Vehicle{
    // dynamic method dispatch happens to these methods
    // same name and signature compared with super class methods
    public void park(){
        System.out.println("Car parks");
    }
    public void drive(){
        System.out.println("Car is driven by a teacher");
    }
}

class Test{
    public static void main(String args[]){
        // create a sub class object using super class reference variable
        System.out.println("-----Dynamic method dispatch-----");
        Vehicle v1 = new Car();
        // method call
        v1.park();
        v1.drive();
        System.out.println();
        System.out.println("-----No dynamic method dispatch-----");
        // create an object using super class reference variable
        Vehicle v2 = new Vehicle();
        // method call
        v2.park();
        v2.drive();
    }
}

Output:



You can see what is happening here...The method in the super class is overridden within the sub class with same name and signature. The functionality of the park method and drive method has been changed dynamically. There's only one function, but different functionalities..For both methods, this is true. Usually this is called the Polymorphism in Object Oriented Programming...

Overriding Rules :

1. Static methods(Access modifier is static) can not be overridden. They are created as template methods. So, Inheritance is not applied to them. Therefore no overriding or dispatch.

2. Private methods can not be overridden since they are only visible to a particular class. It a class has a Private method, it can not be accessed by its sub classes.

3. Final methods also can not be overridden. Actually the purpose of creating a final method is to avoid changing the functionality. If so, how can we override it? Remember it.

4. We can not override a method inside a sub class, with greater access privileges than the super class method. As an example, If a super class method has the access modifier as public, you can not override this inside the sub class with an access modifier has more power than public. It means you can use the same level modifier or lower one as protected, default or private.

5. Both methods in two classes should have the same return type. As an example, you can not have an integer returning method in super class and double returning method in sub class. Both should be int or double.

Another Polymorphism Example :

class Animal {
    int age = 5;
    // super class method 1
    public void eat(){
        System.out.println("Animal eats");
    }
    // super class method 2
    public void life(){
        System.out.println("Animal can live " + age + " years");
    }
}

class Dog extends Animal{
    int age = 4;
    // dynamic method dispatch happens to these methods
    // same name and signature compared with super class methods
    public void eat(){
        System.out.println("Dog eats");
    }
    public void life(){
        System.out.println("Dog can live " + age + " years");
    }
}

class Test{
    public static void main(String args[]){
        System.out.println("Dynamic Method Dispatch happens...");
        Animal dog = new Dog();
        dog.eat();
        dog.life();
    }
}
Output : 

Attributes in the Super class can not be overridden in Polymorphism. See this example.
class Animal { int age = 5; } class Dog extends Animal{ int age = 4; } class Test{ public static void main(String args[]){ Animal dog = new Dog(); System.out.println(dog.age); } }

Output : 

When your program has multi level inheritance, what will happen? Check this one!

class Animal {
    // most super class method
    public void eat(){
        System.out.println("Animal eats food");
    }
}

class Dog extends Animal{
    // dynamic method dispatch happens to this method
    public void eat() {
        System.out.println("Dog eats meat");
    }
}

class Puppy extends Dog{
    // dynamic method dispatch happens to this method
    public void eat() {
        System.out.println("Puppy drinks milk");
    }
}

class Test{
    public static void main(String args[]){
        // create reference variables
        Animal a1, a2, a3;
        System.out.println("-----No Dynamic method dispatch-----");
        a1 = new Animal();
        a1.eat();
        System.out.println("-----Dynamic method dispatch-----");
        a2 = new Dog();
        a3 = new Puppy();
        a2.eat();
        a3.eat();
    }
}

Output :

Here when dynamic method dispatch happens, a1, a2, a3 reference variables are looking at the eat method in the super class Animal first. Then see a newer version in each sub classes. Then executed the code block in those sub classes...

This is the Polymorphism guys!!! Do you get the idea? If so, great! I tried my best to explain you..
But if you have any problem, drop a comment here or to my FB page.
FB page : TechPool

Good Bye guys!!!




5 Comments