Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Difference between Abstraction and Polymorphism in Java and OOP [Answer]

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 at them as they are at the moment, which gives your code flexibility to deal with the changes coming in the 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 cars or bikes or would you think about different kinds of vehicles? 

This is where Abstraction comes into the picture, it allows you to think in terms of a Vehicle rather than thinking in terms of Car. It provides that generalization much needed for the 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 a certain level. 

Classes 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 themselves but specify contracts about how things will work. In Java, interface and abstract class are used to implement Abstraction, but how things will work? How do you code with abstract things? Well, here Polymorphism helps.

It is the magic that 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 than 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 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.

Difference between Abstraction vs Polymorphism in Java and OOP

1) Abstraction allows a programmer to design software better by thinking in general terms rather than specific terms 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, which 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.

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 another. Think what value Abstraction will provide if Polymorphism is not there to take advantage of Abstraction because it is the Polymorphism that 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.

If you want to learn more about Object-Oriented Programming, check out these amazing articles :
  • 10 Free Courses to learn Object Oriented Programming (free courses)
  • 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)
  • What is 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 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)

Thanks for reading this article so far. If you like this object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

And lastly one question for you? What is SOLID principle? Can you name all the 5 object oriented principle which are part of SOLID? 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.