Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Dynamic Polymorphism in Java

In this article we will learn about dynamic polymorphism and how it is implemented in Java

Polymorphism is an important feature of object oriented programming.

In this tutorial we discuss meaning one of the type of polymorphism, that is dynamic polymorphism and its implemented in Java as a programming language.

Our tutorial comprises of three parts mainly:

  1. Description of polymorphism briefly
  2. Second part explains dynamic polymorphism and how it can be achieved
  3. Discuss program written in Java that implements the dynamic polymorphism.

Polymorphism

It is one of the important features of object oriented paradigm. Polymorphism means one name and many forms. In computer science we refer polymorphism as a capability of a method to have multiple definitions.

It allows you to define multiple methods with the same name but various definitions.

In OOP there are two types of polymorphism depending upon how method call is resolved:

  • Static polymorphism
  • Dynamic polymorphism

In this article we will be discussing dynamic polymorphism and its implementation.

Dynamic polymorphism

What is dynamic polymorphism?

Dynamic polymorphism is also called as the run time polymorphism.

As we mentioned earlier while defining polymorphism that polymorphism means there are multiple definitions of a single method. That means a method with same name is defined multiple times in a program. In dynamic polymorphism, if we make a call to such a multiple times defined method in our code then which definition of that method is to be called actually and executed is resolved at run time only.

We can define it as “In dynamic polymorphism the actual method call is resolved dynamically that is at run time only and not at the compile time. “ The compiler only checks the compile time error and does not bind the method call with any actual method. This method binding if is done at runtime by runtime environment then we say dynamic polymorphism is being implemented in the program.

Dynamic polymorphism is achieved by a concept called as method overriding in OOP languages like C++ and Java. Method overriding is redefining the same method again and again in the derived classes in an inheritance hierarchy of the classes.

In next section we discuss in detail themeaning of the term method overriding that is responsible for the behaviour of dynamic polymorphism. And then we further discuss how the code should be written to actually make this method overriding work properly.

Method overriding

Method overriding is a technique to implement dynamic polymorphism in OOP languages in C++ and Java.

Method overridingsimply means is to define a method with same name and signature in the base class as well as in the derived class.

That means you define a method in the base class. Then you redefine the same method in the derived class also then we call that the method is overridden by the derived class.

Here we must first understand what we mean by same method. When we define a method it has three parts return type, name and arguments type. For example:

int  add(int, int);

Then for this method:

  • int - return type
  • add - name of the method
  • int, int - argument passed

Now if we define such a method in base class and then also in the derived class exactly the same but keeping the implementation different. Then we say that add method is an overridden method with respect to those classes.

Implementation of Dynamic polymorphism using method overriding

In this section we discuss an example of Java to explain the method overriding.

Example: Consider a class hierarchy as follows:

Base class is named as base

There are two derived classes as derived1 and derived2

Now suppose we want to have the display method in the all three classes then we will write the following code:

class base  {
	
	// Consturctor for the base class
base() {

}
	
	// display method defined in the base class. Note the return type,
//  name and arguments type passed to the method
public void display (int i) {
	System.out.println (“I am in base class display method” + i);
}
}
	
class derived1 extend base  {

derived1() {
}
// display() method redefined in the derived1 class. Note the return type,
//  name and arguments type passed to the method. 
// The signature of this display method is exactly the same as defined in the base class

	public void display (int i) {
	System.out.println (“I am in derived1 class display method” + i);
}
}

class derived2 extends base {	
derived2() {
}
// display() method redefined in the derived2 class. Note the return type,
//  name and arguments type passed to the method. 
// The signature of this display method is exactly the same as defined in the base class

	public void display (int i) {
	System.out.println (“I am in derived2 class display method” + i);
}
}

Now let us understand this program.

Class base has a method defined as void display (int i). Now the same method is defined there in the derived class derived1. Similarly the same method is defined in the derived class derived2. So the method which is defined in the base class is redefined in the derived classes derived1 and derived2. Hence here we can say that the method display() which is originally defined in the base class is overridden in the derived classes of the base class.

Hence display is an overridden method and will exhibit the behaviour of the dynamic polymorphism. But how? That’s the question we will explore in further section.

Now suppose we use the classes base, derived1 and derived2 as follow in the User class that defines the main method:

class User {
	public static void main (String args[]) {
	base objbase = new base();
	//This will call the method display of base class
	objbase.display();
	objbase = new derived1();
	//This will call the method display of derived1 class
	objbase.display();
	//This will call the method display of derived2 class
	objbase = new derived2();
	objbase.display();
	}
}

In line no. 1 we define an object objbase of type base. We also create a new object of base class and assign the created base class object to the objbase object. In line 3, we call the method display with object objbase. Here in line 3 the method of base class will be called. This looks obvious. But this is because the object actually created by new in line 1 is of base class.

Now in line 4, we create a new object of class derived1. But note that the created object is assigned to the objbase which is of type base class and not of derived1 class. In this case when we call the method display in line 6, then the method of base class is not called but the method of derived1 class is called. This is the magic of the dynamic polymorphism.

Similarly at line 8 we create and assign the object of derived2 class and assign it to the objbase. In this caseat line no. 9 the display method of derived2 class will be called.

So how does this all happen? This is where the concept of dynamic polymorphism plays a role. At compile time such calls are not resolved. These method calls are resolved at the time of program execution. At the time of program execution, the runtime environment checks that the object actually created is that of the derived1 class at line number 4. At line number 6 when display is called objbase is actually having the object of derived1 class though objbase is defined of type base class. Runtime environment resolves the method calling to the class of which the actual object is being created. So the display method call is resolved to calling the method of derived1 class.

Similarly, at line number 8, the same object objbase is assigned the object of class derived2. Then in this case againthe method display call at line number 9 will not be resolved to base class method. But the display method of derived2 class is called.

So what is the final crux now?

  • We must remember that we can create and assign the object of derived class to the variable of type base class. This is completely allowed. That is what we did in lines 4and 8.
  • Now when we call a method on the base class object, the runtime checks the actual object that is being assigned to the base class object. The method of the class whose object is actually created is called and not the method of the class whose object is defined. That is the meaning of dynamic polymorphism.

We must understand here how the behaviour of the object is changing at the runtime. How method call is diverted to the method call of another classes using a single object.

Here is one important point. What if we do not define the method display in the derived2 class and run the following code:

  • base objbase = new derived2();
  • objbase.display();

Now here at line number 1, the objbase is of type base but the object created at the right hand side is of the derived2 class. At line number 2 display method is called. Now we say that display method is not overridden in the derived2 class. Then what will be the behaviour of line 2. In that case the display method of the base class will be called.

So in nutshell, we can say that if the method is redefined in the derived class, then the method of derived class is called. Otherwise the derived class inherits the method from the base class and the method of base class is called.

Happy Reading!



computer science Graduate :Ceo at www.gigsclerk.com (Best Marketplace to Buy and sell service online from 2$-100$ ) and www.trustingeeks.com. Interested in programming, online buissiness. Android development.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login