Difference between Abstraction and Polymorphism in Java OOPS

Abstraction and Polymorphism are very closely related and understanding  the difference between them is not as easy as it looks. Their operating model is also very similar and based upon the relationship of parent and child classes. In fact, Polymorphism needs the great support of Abstraction to power itself, without Abstraction you cannot leverage the power of Polymorphism. Let's understand this by what Abstraction and Polymorphism provide to an object oriented program. Abstraction is a concept to simplify the structure of your code. Abstraction allows you to view things in more general terms rather than looking them as they are at the moment, which gives your code flexibility to deal with the changes coming in future.

For example, if you were to design a program to control vehicles e.g. starting, stopping, horn, accelerator, breaks etc, how do you do that? would you design your program just to work with car or bike or would you think about different kinds of vehicles? This is where Abstraction comes into the picture, it allows you think in terms of Vehicle rather than thinking in terms of Car. It provides that generalization much needed for a software to be reusable and customizable.

Now let's understand what Polymorphism provides you? Abstraction is nothing but creating a different class to handle details up to certain level. Class with a higher level of Abstraction will deal with most general details and classes at the lowest level of Abstraction will deal with most specific details.

At the top of Abstraction, we have complete abstract things which don't do anything by itself but specify contracts about how things will work. In Java, interface and abstract class is used to implement Abstraction, but how things will work? How do you code with abstract things? Well, here Polymorphism helps.

It is the magic which works at runtime to connect abstract things with a concrete implementation. This allows you to code with abstract class and interface, which results in much more flexible and maintainable code then directly working with concrete classes. At runtime, Java identifies which concrete class's object is available and then it calls the implementation of the abstract method from that class.

Difference between Abstraction and Polymorphism in Java

In the last paragraph, I tried to explain how Abstraction and Polymorphism work together to provide a flexible and maintainable software but to understand them better let's do comparative analysis by analyzing syntactical and the semantical difference between Polymorphism and Abstraction in object oriented programming.

1) Abstraction allows a programmer to design software better by thinking in general terms rather than specific term while Polymorphism allows a programmer to defer choosing the code you want to execute at runtime.

2) Another difference between Polymorphism and Abstraction is that Abstraction is implemented using abstract class and interface in Java while Polymorphism is supported by overloading and overriding in Java.

3) Though overloading is also known as compile time Polymorphism, method overriding is the real one because it allows a code to behave differently at different runtime conditions, what is known as exhibiting polymorphic behavior.

I strongly suggest reading Head First object oriented Analysis and design as well as Head First Design pattern to understand the object oriented programming better. Both books will explain to you how to use the OOP principles and basics in the right way.

Abstraction vs Polymorphism in Java and OOP

That's all about the difference between Abstraction and Polymorphism in Java. They are not a perpendicular concept, in fact they are very closely related and one cannot survive without other. Think what value Abstraction will provide if Polymorphism is not there to take advantage of Abstraction because it is the Polymorphism which links abstract code to concrete implementation. Similarly, if there would be no Abstraction, no parent and child classes then there would not be any polymorphic code.
Difference between Abstraction vs Polymorphism in Java and OOP

Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming in Java
Java - Object Oriented Programming [For Absolute Beginners]

If you want to learn more about Object Oriented Programming, check out these amazing articles :
  • What is the difference between Class and Object in Java or OOP? (answer)
  • The difference between abstract class and interface in Java? (answer)
  • The difference between Abstraction and Encapsulation in Java? (answer)
  • The difference between Inheritance and Polymorphism in Java? (answer)
  • 10 Object Oriented Design Principles Every Programmer Should know? (answer)
  • The difference between Aggregation, Composition, and Association in OOP? (answer)
  • The difference between State and Strategy design patterns? (answer)
  • The difference between Factory and Abstract Factory design patterns? (answer)
  • What is Inheritance in Java? (answer)
  • What is Encapsulation in Java? (answer)
  • What is Polymorphism in Java? Overloading or Overriding? (answer)
  • What is the difference between Overloading and Overriding in OOP? (answer)
  • The difference between instance and object in Java? (answer)
  • What is the difference between static and dynamic binding in Java? (answer)

No comments:

Post a Comment