Runtime Polymorphism in Java with Example

The simple meaning of polymorphism is many forms. In Java, polymorphism is the process of performing a task in various different methods. It is the ability of an object that helps them take various different forms. It can be achieved through method overriding and method overloading. There are two kinds of Polymorphism in Java. They are runtime polymorphism and compile time polymorphism. Method overloading in the static method is a type of compile time polymorphism. In this post, we will deal with runtime polymorphism.

Runtime polymorphism in Java

Runtime polymorphism is achieved in Java through method overriding. It is a process in which a method overriding call in resolved at runtime, not at compile time.

It is also known as Dynamic Method Dispatch. When an overridden method is called through a parent class reference, Java determines whether to execute the parent class method or the child class method.

This decision is taken by Java on the basis of the object being referred to at the time the call occurs. This whole process takes place at runtime, not at compile time. In this process, a parent class reference variable can also refer to a child class object. This phenomenon is known as upcasting.

// 1st class

public class A {

	void methodOne() {
		System.out.println("This is super class");
	}
}

// 2nd class

public class B extends A()
{
	void

	void methodOne() {
		System.out.println("this is sub class");
	}
}
// 3rd class

public class Test()
{
public static void main(String[] args) {
	
	A obj = new (B);
	obj.methodOne();
	}
}

Runtime Polymorphism

 

Upcasting

When reference variable of a superclass refers to the object of a subclass, it is called upcasting. It is not usually necessary; however, upcasting is used to write code that only deals with the superclass.

Static binding and Dynamic binding

In Java, binding is the process if connecting a method call to a method body. There are two types of binding. They are Static binding/ Early binding, and Dynamic binding/ Late binding.

Understanding types of instance

  1. Variables have a type: Each variable can be divided into two types: primitive and non-primitive.
  2. References have a type
  3. Objects have a type: An object is an instance of a distinct Java class, but it is also an instance of its parent class.

Static binding

Static binding is the binding which is resolved at the compile time. It is used by private, final, and static methods and variables. It is better than dynamic minding in case of performance because it does not require extra overhead. It is bounded by the compiler during the compile-time. The compiler is always accessible to object of local class so it doesn’t have any difficulty to determine the object of the class. Type information is used by the static binding for binding. Method overloading is a type of static binding. It is also known as early binding.

Dynamic binding

Dynamic binding is the binding in which type of object is determined at runtime. In dynamic binding, it is not the compiler that decides the method to be called. Method overriding is a type of dynamic binding which is bonded during runtime based upon the type of runtime object. In this method, both subclass and superclass have the same method. Objects are used by the dynamic binding for binding. It is also known as late binding.

Java instanceof

Java instanceof operator is used to check the class of the object. It is used for checking whether a reference variable contains a given type of object reference or not. Java instanceof keyword is called comparison operator as the keyword makes a comparison between instances with the types.

However, the class of the object cannot be checked dynamically using Java instanceof operator. It checks whether a Boolean expression is true or false. The keyword gives a compile-time error if the object with other classes which it doesn’t instantiate are checked.

Instanceof operator used with a variable that has a null value

If the Java instanceof operator is used with a variable with a null value, then it returns false.

Downcasting with Java instanceof operator

When reference variable of a subclass refers to the object of a superclass, it is called downcasting.  Downcasting is only possible through the use of Java instanceof operator.

If we do it directly, we face compile time error and if we use typecasting, then we face CalssCastException error. It involves a type check. Downcasting is used more regularly than upcasting and it is used to access specific behaviors of a child class.

Leave a Reply

Your email address will not be published. Required fields are marked *