Monday, 30 January 2017

POLYMORPHISM IN JAVA

Defining a method in multiple classes with the same name with different implementations for exhibiting different behaviors of the object is called polymorphism. We can develop polymorphism by using –

  • ·        Method overriding or
  • ·        Method overloading


To develop polymorphism, we must define method in all subclasses with the same name with the same prototype as it is declared in the super class.

Types of polymorphism –

1. Compile time polymorphism or static binding or early binding – Whenever a method is invoked, and its method definition is bind at compilation time by compiler is only executed by jvm at runtime, then it is called compile-time polymorphism or static binding or early binding. Static methods, overloaded methods and non-static methods which are not overridden in sub class come under compile time polymorphism.

2. Runtime polymorphism or dynamic binding or late binding – When a method is invoked, the method definition which is bind at compilation time is not executed at runtime, instead if it is executed from the sub class based on the object stored in the referenced variable is called runtime polymorphism. Only non-static overridden methods come under runtime polymorphism. Private non-static methods and default non-static methods from outside package are not overridden, so these method call comes under compile time polymorphism.

No comments:

Post a Comment