Skip to content

OOP Polymorphims Introduction#

What Is The Polymorphims In OOP?#

  • Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
  • Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
  • It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed.
  • The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object.
  • A reference variable can refer to any object of its declared type or any sub-type of its declared type. A reference variable can be declared as a class or interface type.

Polymorphims Example#

Animal.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package com.java.core.polymorphims;  

public class Animal {  

    private String name;  

    public String getName() {  
        return name;  
    }  

    public void setName(String name) {  
        this.name = name;  
    }  

    @Override  
    public String toString() {  
        return "Animal{" +  
                "name='" + name + '\'' +  
                '}';  
    }  
}
Vegetarian.java
1
2
3
4
5
6
7
package com.java.core.polymorphims;  

public interface Vegetarian {  

    String eatVegetables();  

}
Deer.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package com.java.core.polymorphims;  

public class Deer extends Animal implements Vegetarian {  

    @Override  
    public String eatVegetables() {  
        return this.getName() + " eating grass!";  
    }  

}
  • Now, the Deer class is considered to be polymorphic since this has multiple OOP Inheritance. Following are true for the above examples −

    • A Deer IS-A Animal
    • A Deer IS-A Vegetarian
    • A Deer IS-A Deer
    • A Deer IS-A Object
  • When we apply the reference variable facts to a Deer object reference, the following declarations are legal.

JavaPolymorphimsMain.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package com.java.core.polymorphims;

public class JavaPolymorphimsMain {

    public static void main(String[] args) {
        Deer deer = new Deer();
        deer.setName("Deer");
        System.out.println(deer.eatVegetables());

        Animal animal = deer;
        System.out.println(animal.getName());

        Vegetarian vegetarian = deer;
        System.out.println(vegetarian.eatVegetables());

        Object object = deer;
        System.out.println(object);
    }

}
  • Then you will see the result as below:
1
2
3
4
Deer eating grass!
Deer
Deer eating grass!
Animal{name='Deer'}
  • Now let do another example with with Polymorphims about virtual method. See example below.
Employee.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.java.core.polymorphims;  

public class Employee {  

    private String name;  
    private String id;  
    private double salary;  

    public Employee(String name, String id, double salary) {  
        this.name = name;  
        this.id = id;  
        this.salary = salary;  
    }  

    public double calculateSalary() {  
        return salary;  
    }  

    public String getName() {  
        return name;  
    }  

    public void setName(String name) {  
        this.name = name;  
    }  

    public String getId() {  
        return id;  
    }  

    public void setId(String id) {  
        this.id = id;  
    }  

    public double getSalary() {  
        return salary;  
    }  

    public void setSalary(double salary) {  
        this.salary = salary;  
    }  
}
Developer.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
package com.java.core.polymorphims;

public class Developer extends Employee {

    public Developer(String name, String id, double salary) {
        super(name, id, salary);
    }

    @Override
    public double calculateSalary() {
        return this.getSalary() + 100;
    }

}
JavaPolymorphimsMain.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package com.java.core.polymorphims;

public class JavaPolymorphimsMain {

    public static void main(String[] args) {

        Employee developer = new Developer("Duc", "12345", 100);
        System.out.println("Duc " + developer.calculateSalary());

        Employee employee = new Developer("Han", "56789", 200);
        System.out.println("Han " + employee.calculateSalary());

        Employee director = new Employee("John", "55555", 100);
        System.out.println("John " + director.getSalary());

    }

}
  • Now, let's run test and you will see the result as below:
1
2
3
Duc 200.0
Han 300.0
John 100.0
  • As you can see, so base on creating Employee or Developer by new keyword, the method calculateSalary would be different at the runtime.
  • This behavior is referred to as virtual method invocation, and these methods are referred to as virtual methods. An overridden method is invoked at run time, no matter what data type the reference is that was used in the source code at compile time, It will choose the data type that defined after new keyword.

See Also#

References#