Java Polymorphism - Java

What is Java Polymorphism?

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 which can skip multiple IS-A test is taken into consideration to be polymorphic. In Java, all Java items are polymorphic due to the fact any item will skip the IS-A test for their own type and for the class object.

it is critical to realize that the most effective possible way to access an item is thru a reference variable. A reference variable can be of only one type. once declared, the type of a reference variable can't be changed.

The reference variable can be reassigned to other objects provided that it is not declared final. The form of the reference variable could decide the techniques that it can invoke on the item.

A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.

Example

Let us lookat an example.

Now, the Deer class is considered to be polymorphic since this has multiple 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 −

Example

all the reference variables d, a, v, o refer to the same Deer object in the heap.

Virtual methods

on this section, i will show you ways the conduct of overridden methods in Java permits you to take benefit of polymorphism when designing your classes.

We already have discussed method overriding, where a child class can override a method in its parent. An overridden approach is basically hidden inside the parent class, and is not invoked unless the child class uses the super keyword within the overriding method.

Example

Now suppose we extend Employee class as follows −

Now, you study the following program carefully and try to determine its output −

This will produce the following result −

Output

Here, we instantiate two Salary objects. One using a Salary reference s, and the other using an Employee reference e.
While invoking s.mailCheck(), the compiler sees mailCheck() in the Salary class at compile time, and the JVM invokes mailCheck() in the Salary class at run time.

mailCheck() on e is quite different because e is an Employee reference. When the compiler sees e.mailCheck(), the compiler sees the mailCheck() method in the Employee class.

right here, at compile time, the compiler used mailCheck() in employee to validate this statement. At run time, but, the JVM invokes mailCheck() in the salary class.

This conduct is referred to as virtual method invocation, and these strategies 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.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Topics