X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Polymorphism :

Polymorphism is a process of representing 'one form in many forms'.

It is not a programming concept but it is one of the principle.

In object oriented programming, we have two types of polymorphism.

They are compile time polymorphism and run time polymorphism.

Java does not support compile time polymorphism but Java supports only run time polymorphism (implemented by using method overriding concept).


Compile time Polymorphism :

Method overloading exhibit compile time polymorphism. As selection of method is done during compile time based on number of parameters, types of parameters or order of parameters. The method which is selected by compiler, same method is executed by JVM during runtime. As behaviour of this method is changed at compile time, it is called compile time polymorphism.

Example 1 :

class Account { private int accno; private String name; private float balance; void setAccount(int accno , String name) { this.accno = accno; this.name = name; } void setAccount(int accno , String name , float balance) { setAccount(accno , name); this.balance = balance; } String getAccount() { return accno + " " + name + " " + balance; } } class Bank { public static void main(String [ ] args) { Account acc1 = new Account(); Account acc2 = new Account(); acc1.setAccount(101 , "John"); acc2.setAccount(102 , "Jack" , 5000f); String a1 = acc1.getAccount(); String a2 = acc2.getAccount(); System.out.println(a1); System.out.println(a2); } }

Output :

//save as : Bank.java //compile as :javac Bank.java //run as : java Bank 101 John 0.0 102 Jack 5000.0

Example 2 :

class A { void print(double d) { System.out.println("Inside Double"); } void print(float f) { System.out.println("Inside Float"); } } class B { public static void main(String [ ] args) { A obj1 = new A(); obj1.print(10.0); } }

Output :

//save as : B.java //compile as :javac B.java //run as : java B Inside Double

Example 3 :

class A { void print(double d) { System.out.println("Inside Double"); } void print(float f) { System.out.println("Inside Float"); } } class B { public static void main(String [ ] args) { A obj1 = new A(); obj1.print(10.0f); } }

Output :

//save as : B.java //compile as :javac B.java //run as : java B Inside Float

Example 4 :

class A { void print(double d) { System.out.println("Inside Double"); } void print(float f) { System.out.println("Inside Float"); } } class B { public static void main(String [ ] args) { A obj1 = new A(); obj1.print(10); } }

Output :

//save as : B.java //compile as :javac B.java //run as : java B Inside Float

Dynamic Binding :

Dynamic binding always refers not to create the object of the derived class rather creates an object of the base class.

Dynamic binding principle always used for executing polymorphic based applications.

The process of binding appropriate versions (overridden methods) of derived classes which are inherited from base class with base class object is known as dynamic binding.

The advantages of polymorphism along with method overriding and dynamic binding are :

  • Application will take less memory space

  • Application will take less execution time

  • Application gives more performance

  • If we use dynamic binding principle for executing polymorphic based applications, internally JVM will consider the following two points :

  • what type of object

  • what type of reference/address object contains

In order to create an object indirectly, there must exist base - derived relationship.

If we create an object directly then such object creation is known as plain old execution logic and it is not necessary to have base - derived relationship.

The following set of executable statements which as used for executing the program represented by 
making use of dynamic binding principle.

class Base
{
 void m1()
 {
 
 }
}
class Derive1 extends Base
{

}
class Derive2 extends Base
{

}
class Example
{
 public static void main(String[ ] args) 
 {
   Base b = new Base(); // Line 1
   b.m1();
   
   b = new Derive1(); // Line 2
   b.m1();
   
   b = new Derive2(); // Line 3
   b.m1();
 }
}

In the above example in Line 1, Line 2 and Line 3 the object 'b' contain reference of Base, Derive1 and Derive2 classes. Hence, 'b' object is known as polymorphic object. The method m1() is actually available in one form (original form) and it is implemented in multiple forms (overridden methods). Hence, m1() method is known as polymorphic method.


Case 1 :

Business logic will be developed in independent classes and execution logic will be the developed with plain old execution logic (direct object creation).


Case 2 :

Business logic will be developed in dependent classes and execution logic developed with plain old execution logic (direct object creation).

If we develop any Java application with above two cases then we get the following limitations :

  • application takes more time more memory space

  • application execution time is more

  • application performance is reduced

Case 3 :

Business logic will be developed in dependent classes (contains polymorphism and method overriding) and execution logic will be developed by using dynamic binding principle.

If we develop any Java application by using Case 3 then we will get the following advantages :

  • application takes less memory space

  • application execution time is less

  • application performance is enhanced

In static polymorphism the overloaded methods bind with an object at compile time by differentiating its signature. Due to static polymorphism we get poor utilization of the resources (especially main memory). In dynamic polymorphism the methods are binding at runtime by differentiating their signature.

Even though C++ satisfies both static and dynamic polymorphism, C++ developer never follow static polymorphism because of its limitation and they always follow dynamic polymorphism. In C++ static polymorphism principle is implemented by the function overloading and operator overloading. In C++ dynamic polymorphism will be implemented by virtual and pure virtual functions.

Even though function overloading concept is same in both C++ and Java, in C++ overloaded methods will be binding with an object at compile time whereas in Java programming overloaded methods bind with an object at runtime. Due to the limitation of static polymorphism Java never follows static polymorphism but it always follows dynamic polymorphism.



X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.