The Best Technical Blog Ever

twitterrss

Polymorphism in Java

Polymorphism is one of the major building blocks of object oriented programming. The word "Polymorphism" comes from ancient Greek where poly means many. So polymorphism means the ability to take more than one form. In simple words, the same operations may behave differently on different classes. So we can easily create functions or reference variables (same operations) which behaves differently in different programmatic context. It allows us to write generic, reusable code more easily. Because we can specify general instructions in parent class so that subclasses of the class can define their own unique behaviors and yet share some of the functionality of the parent class.


Any object that satisfies more than one IS-A relationship is polymorphic in nature. Polymorphism is extensively used in implementing inheritance. Because, In object oriented programming, IS-A relationship is fully based on Inheritance concept. For example, Dog is a Mammal, Bus is a Vehicle , Circle is a Shape etc. Here, Circle satisfies the IS-A relationship for its own type as well as its parent class Shape. Also Inheritance is uni-directional. For example, Circle is a Shape. But Shape is not a Circle. In Java, all Java objects are polymorphic; since each one passes an IS-A test for itself and also for Object class.


Polymorphism


Polymorphism in Java has two types:

  • Compile time polymorphism/Static polymorphism
  • Runtime polymorphism/Dynamic polymorphism

Compile time polymorphism:

Compile time polymorphism is achieved through method overloading. Method overloading occurs when two or more methods of a class have the same method name but different parameters. These overloaded methods are bonded using static binding. Here, actual called method is decided at compile-time, based on the method signatures (number and types of arguments).


Example:
java overload


In the above example, first add method takes two integer parameters while the second one takes three integer parameters. But for the third add method, there is a change in parameter types. In compile time, compiler checks at the method signature and decides which method to invoke for a particular method call. For more details refer method overloading in java.


Runtime polymorphism:

Runtime polymorphism is achieved through method overriding. Here, a call to an overridden function is resolved at a Run-Time. That means, when an overridden method is called by a reference, java determines which method to execute based on the type of object it refer to in run time. Even if we have a variable with the type of a parent class, we can assign it to a child class object and we can call overridden methods from the child class using that variable.


Example:
polymorphism
polymorphism


In this program, first we creates an object of the subclass1 (Circle) and assigns it to the superclass (Shape) reference. When we call the overridden method draw() through the superclass reference, the subclass version of the method will be called. It should be noted that in the first call to draw(), the reference type is Shape and the object being referenced is Circle. So, when a call to draw() is invoked, Java waits until runtime to determine which object is actually being pointed to by the reference. In this case, the object is of the class Circle. So, the draw() method of Circle class will be called. In the second call to draw(), object being referenced is Square. So, the draw() method of Square class will be called. In the third call to draw(), the object is of the class Shape. So, the draw() method of Shape will be called.


When parent class reference variable refers to Child class object, it is known as Upcasting. In General, reference variable of parent class X can hold a reference to an object of class X or an object belonging to any subclasses of class X. Here, the type of object on which method is being invoked is not known at compile time but will be decided at run time based on the type of actual object stored in that reference variable and not on the type of the reference variable. That's why it is called as runtime polymorphism. We can override only methods not data members. So Run time polymorphism can't be achieved by data members. If we try to access the child class data member using the parent class reference variable, it will always access the data member of parent class. For more details refer method overriding in java.


Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status