java polymorphism tutorial

Bodhak java polymorphism tutorial in this chapter we will discuss What is Polymorphism?,  Advantage of Polymorphism, compile-time polymorphism, Runtime Polymorphism, Dynamic Binding, Dynamic Bending vs Static Binding, Commandline arguments.

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




java polymorphism tutorial

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.


There are 3 distinct forms of Java Polymorphism:

Method overloading (Compile time polymorphism)

The Method overriding through inheritance (Run time polymorphism)

Method overriding through the Java interface (Run time polymorphism)

The advantage of Polymorphism:


  • If you need to write code that deals with a family of types, the code can ignore type-specific details and just interact with the base type of the family
  • Even though the code thinks it is using an object of the base class, the object’s class could actually be the base class or any one of its subclasses
  • This makes your code easier for you to write and easier for others to understand


Other subclasses could be added later to the family of types, and objects of those new subclasses would also work with the existing code

Compile time polymorphism:

Method overloading is a Compile time Polymorphism. As t we can have multiple subtype implementations for a supertype, the compiler determines which type to be invoked at the compile time.

Runtime polymorphism

Method overriding is a run-time polymorphism.

Object Slicing:

Subclass object contains properties & behavior of the superclass and its own properties & behavior.

Superclass reference variable can invoke the only methods/behavior of super class inherited in subclass

Next Page