Runtime Polymorphism in java

Polymorphism in java is a concept by which we can perform a single action by different ways

Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time.

 class Car {

	void run() {
		System.out.println("running");
	}

}

class Honda extends Car {

	void run() {
		System.out.println("running with 40km");
	}

	public static void main(String args[]) {
		Car c = new Honda();// upcasting
		c.run();
	}
}
 

Real life example of polymorphism

Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person present in different-different behaviors. In java programming the polymorphism principal is implemented with method overriding concept of java. We know that method overriding is equal to method heading is some method body is different.

Polymorphism principal is divided into two sub principal they are:

1.Static / Compile time polymorphism

2.Dynamic / Runtime polymorphism

Java programming does not support static polymorphism because of its limitations and java always supports dynamic polymorphism. Let us consider the following diagram

poly

In the above diagram the sum method which is present in BC class is called original form and the sum() method which are present in DC1 and DC2 are called overriden form hence Sum() method is originally available in only one form and it is further implemented in multiple forms. Hence Sum() method is one of the polymorphism method.

Static polymorphism

It is one of the sub principal of polymorphism one of the object oriented programming language supports even static polymorphism. The language C, C++ supports static polymorphism principal by using its C++ programming concepts called function overloading and operator overloading as per as standalone mechanism are concerned there is no difference in performance of static polymorphism and dynamic polymorphism application but there is a significant difference in execution of there applications in busy environment like client server environment.

Definition

The process of finding the overloaded method within object at compile time is known as static polymorphism due to static polymorphism utilization of resources (main memory space) is poor because for each and every overloaded method a memory space is created at compile time when it binds with an object. In C++ environment the above problem can be solve by using dynamic polymorphism by implementing with virtual and pure virtual function so most of the C++ developer in real worlds follows only dynamic polymorphism.

Dynamic polymorphism

In dynamic polymorphism method of the program binds with an object at runtime the advantage of dynamic polymorphism is allocating the memory space for the method (either for overloaded method or for override method) at run time.

Conclusion

The advantage of dynamic polymorphism is effective utilization of the resources effectively java programming does not support static polymorphism because of its limitation. It is always dynamic polymorphism because of its advantages.

Java Runtime Polymorphism with Multilevel Inheritance

Let's see the simple example of Runtime Polymorphism with multilevel inheritance.

						 class Animal {
	void eat() {
		System.out.println("eating");
	}
}

class Dog extends Animal {
	void eat() {
		System.out.println("eating fruits");
	}
}

class BabyDog extends Dog {
	void eat() {
		System.out.println("drinking milk");
	}

	public static void main(String args[]) {
		Animal a1, a2, a3;
		a1 = new Animal();
		a2 = new Dog();
		a3 = new BabyDog();

		a1.eat();
		a2.eat();
		a3.eat();
	}
}
Output: eating
        eating fruits
        drinking Milk