Polymorphism is an important Object oriented concept and widely used in Java and other programming language. Polymorphism in java is supported along with other concept like abstraction, encapsulation and inheritance. bit on historical side polymorphism word comes from ancient Greek where poly means many so polymorphic are something which can take many form. In this java polymorphism tutorial we will see what polymorphism in Java is, how polymorphism is implemented in Java, why should we use polymorphism and how can we take advantage of polymorphism while writing code in Java. Along the way we will also see a real world example of using polymorphism in Java.
By the way I learned about abstraction, encapsulation and polymorphism during my college time but never able to recognize its real potential until I started doing programming and involved in bigger projects. On theory Polymorphism is a simple concept where One variable can denote multiple object but in real life it just a gem and a code written using polymorphism concept is much flexible to change and quite easy to maintain than the one which is written without polymorphism.
What is polymorphism in Java
Polymorphism is an Oops concept which advice use of common interface instead of concrete implementation while writing code. When we program for interface our code is capable of handling any new requirement or enhancement arise in near future due to new implementation of our common interface. If we don't use common interface and rely on concrete implementation, we always need to change and duplicate most of our code to support new implementation. Its not only java but other object oriented language like C++ also supports polymorphism and it comes as fundamental along with encapsulation , abstraction and inheritance.
How Polymorphism supported in Java
Java has excellent support of polymorphism in terms of Inheritance, method overloading and method overriding. Method overriding allows java to invoke method based on a particular object at run-time instead of declared type while coding. To get hold of concept let's see an example of polymorphism in java:
class TradingSystem{
public String getDescription(){
return "electronic trading system";
}
}
class DirectMarketAccessSystem extends TradingSystem{
public String getDescription(){
return "direct market access system";
}
}
class CommodityTradingSystem extends TradingSystem{
public String getDescription(){
return "Futures trading system";
}
}
Here we have a super class called TradingSystem and there two implementation DirectMarketAccessSystem and CommodityTradingSystem and here we will write code which is flexible enough to work with any future implementation of TradingSystem we can achieve this by using polymorphism in java which we will see in further example.
Where to use Polymorphism in code
Probably this is the most important part of this java polymorphism tutorial and It’s good to know where you can use polymorphism in java while writing code. Its common practice to always replace concrete implementation with interface it’s not that easy and s comes with practice but here are some common places where I check for polymorphism:
1) Method argument:
Always use super type in method argument that will give you leverage to pass any implementation while invoking method. For example:
public void showDescription(TradingSystem tradingSystem){
tradingSystem.description();
}
If you have used concrete implementation e.g. CommodityTradingSystem or DMATradingSystem then that code will require frequent changes whenever you add new Trading system.
2) Variable names:
Always use Super type while you are storing reference returned from any Factory object, this gives you Flexibility to accommodate any new implementation from Factory. Here is an example of polymorphism while writing java code which you can use retrieving reference from Factory:
String systemName = Configuration.getSystemName();
TradingSystem system = TradingSystemFactory.getSystem(systemName);
Method overloading and method overriding in Java
Method is overloading and method overriding uses concept of polymorphism in java where method name remains same in two classes but actual method called by JVM depends upon object. Java supports both overloading and overriding of methods. In case of overloading method signature changes while in case of overriding method signature remains same and binding and invocation of method is decided on runtime based on actual object. This facility allows java programmer to write very flexibly and maintainable code using interfaces without worrying about concrete implementation. One disadvantage of using polymorphism in code is that while reading code you don't know the actual type which annoys while you are looking to find bugs or trying to debug program. But if you do java debugging in IDE you will definitely be able to see the actual object and the method call and variable associated with it.
Parameteric Polymorphism in Java
Java started to support parametric polymorphism with introduction of generic in JDK1.5. Collection classes in JDK 1.5 are written using Generic Type which allows Collections to hold any type of object in run time without any change in code and this has been achieved by passing actual Type as parameter. For example see the below code of a parametric cache written using generic which shows use of parametric polymorphism in Java.
interface cache{
public void put(K key, V value);
public V get(K key);
}
That’s all on polymorphism in java for now, please suggest and share some of other coding practices which involve use o f polymorphic behavior of java for benefit of all. Thank you.
By the way I learned about abstraction, encapsulation and polymorphism during my college time but never able to recognize its real potential until I started doing programming and involved in bigger projects. On theory Polymorphism is a simple concept where One variable can denote multiple object but in real life it just a gem and a code written using polymorphism concept is much flexible to change and quite easy to maintain than the one which is written without polymorphism.
What is polymorphism in Java
Polymorphism is an Oops concept which advice use of common interface instead of concrete implementation while writing code. When we program for interface our code is capable of handling any new requirement or enhancement arise in near future due to new implementation of our common interface. If we don't use common interface and rely on concrete implementation, we always need to change and duplicate most of our code to support new implementation. Its not only java but other object oriented language like C++ also supports polymorphism and it comes as fundamental along with encapsulation , abstraction and inheritance.
How Polymorphism supported in Java
Java has excellent support of polymorphism in terms of Inheritance, method overloading and method overriding. Method overriding allows java to invoke method based on a particular object at run-time instead of declared type while coding. To get hold of concept let's see an example of polymorphism in java:
class TradingSystem{
public String getDescription(){
return "electronic trading system";
}
}
class DirectMarketAccessSystem extends TradingSystem{
public String getDescription(){
return "direct market access system";
}
}
class CommodityTradingSystem extends TradingSystem{
public String getDescription(){
return "Futures trading system";
}
}
Here we have a super class called TradingSystem and there two implementation DirectMarketAccessSystem and CommodityTradingSystem and here we will write code which is flexible enough to work with any future implementation of TradingSystem we can achieve this by using polymorphism in java which we will see in further example.
Where to use Polymorphism in code
Probably this is the most important part of this java polymorphism tutorial and It’s good to know where you can use polymorphism in java while writing code. Its common practice to always replace concrete implementation with interface it’s not that easy and s comes with practice but here are some common places where I check for polymorphism:
1) Method argument:
Always use super type in method argument that will give you leverage to pass any implementation while invoking method. For example:
public void showDescription(TradingSystem tradingSystem){
tradingSystem.description();
}
If you have used concrete implementation e.g. CommodityTradingSystem or DMATradingSystem then that code will require frequent changes whenever you add new Trading system.
2) Variable names:
Always use Super type while you are storing reference returned from any Factory object, this gives you Flexibility to accommodate any new implementation from Factory. Here is an example of polymorphism while writing java code which you can use retrieving reference from Factory:
String systemName = Configuration.getSystemName();
TradingSystem system = TradingSystemFactory.getSystem(systemName);
Method overloading and method overriding in Java
Method is overloading and method overriding uses concept of polymorphism in java where method name remains same in two classes but actual method called by JVM depends upon object. Java supports both overloading and overriding of methods. In case of overloading method signature changes while in case of overriding method signature remains same and binding and invocation of method is decided on runtime based on actual object. This facility allows java programmer to write very flexibly and maintainable code using interfaces without worrying about concrete implementation. One disadvantage of using polymorphism in code is that while reading code you don't know the actual type which annoys while you are looking to find bugs or trying to debug program. But if you do java debugging in IDE you will definitely be able to see the actual object and the method call and variable associated with it.
Parameteric Polymorphism in Java
Java started to support parametric polymorphism with introduction of generic in JDK1.5. Collection classes in JDK 1.5 are written using Generic Type which allows Collections to hold any type of object in run time without any change in code and this has been achieved by passing actual Type as parameter. For example see the below code of a parametric cache written using generic which shows use of parametric polymorphism in Java.
interface cache{
public void put(K key, V value);
public V get(K key);
}
That’s all on polymorphism in java for now, please suggest and share some of other coding practices which involve use o f polymorphic behavior of java for benefit of all. Thank you.
really helpful thank you
ReplyDelete