Polymorphism in Java

Polymorphism in Java.

In this tutorial, we will discuss about Polymorphism.

Polymorphism represents one cell may have multiple forms, one method can have different behaviors.  In our education, class professor has told that poly indicates "many" and  morph means forms or change. From here Polymorphism word has evolved which has many forms.


Polymorphism in Java


Generally, Polymorphism can be referred between child and parent classes.

Child class can have as same as its parent class behavior or can implement its own behavior.

Polymorphism Example:


For example. We have an interface School, class is Student and class Address.
 

public interface School{
}

public class Address{
}

public class Student implements School{

private Address address;

}

In Java, Object class is base or parent class for all classes in java api as well for user defined class.

We can describe Student object as below.

a) Student is a part of School
b) Student has a address.
c) Student is Student
d) Student is Object

We will demonstrate Polymorphism with the following example.
package com.java.w3schools.core.inheritance;

public class Company {

private String companyName;
private String address = "California";

public Company(String cName, String cAddress) {
companyName = cName;
address = cAddress;
}

public String getAwards() {
return "No Awards";
}
public String[] getPolicies(String designation) {
String[] policies = { "HR Policy", "Tax Policy" };
return policies;
}
}

package com.java.w3schools.core.inheritance;

class Employee extends Company {
public Employee(String cName, String cAddress) {
super(cName, cAddress);
}

@Override
public String[] getPolicies(String designation) {
String[] policies = new String[2];
if ("D1".equalsIgnoreCase(designation)) {
policies[0] = "Policy 3";
policies[1] = "Policy 4";
} else if ("D2".equalsIgnoreCase(designation)) {
policies[0] = "Policy 5";
policies[1] = "Policy 6";
} else {
policies[0] = "Policy 1";
policies[1] = "Policy 2";
}
return policies;
}
}

public class EmployeeMain {
public static void main(String[] args) {
Employee employee1 = new Employee("Michael", "California");
String[] polices = employee1.getPolicies("D2");
System.out.println("policy one : " + polices[0]);
System.out.println("policy Two : " + polices[1]);
}
}


Output:


policy one : Policy 5
policy Two : Policy 6

Company class has implementation for getPolicies, but child class provided its defined own implementation. Child class Employee now independent of parent class.

By using following code in Employee class, we can make use Employee class implementation as well parent class implementation for getPolicies method. We have invoked parent class method in last else block.
@Override
public String[] getPolicies(String designation) {
String[] policies = new String[2];

if ("D1".equalsIgnoreCase(designation)) {
policies[0] = "Policy 3";
policies[1] = "Policy 4";
} else if ("D2".equalsIgnoreCase(designation)) {
policies[0] = "Policy 5";
policies[1] = "Policy 6";
} else {
return super.getPolicies(designation);
}
return policies;
}


If we pass apart from "D1", "D2" designation than return parent class method policies.


Output:


policy one : HR Policy
policy Two : Tax Policy

From above code, we observed that behavior is changing during runtime upon input. That returns either child or parent class getPolicies method String array.

We will see 

1) Compile time Polymorphism(Overloading)
2) Runtime Polymorphism(Overriding)

0 Comments