Code Reusability and Inheritance

Share on:

Overview

In this article we'll see a story that guides us towards inheritance. We'll look at different use cases and look at UML diagrams for those use cases to understand the proposed solutions. Let's dive deep

Story

Let's say we are designing a system in which we have different shapes, namely, Square, Circle, Triangle and Rectangle. We can call two methods on these shapes: rotate() and playSound() and assume that the implementation of these functions in same for different shapes. Is the story clear till here?
[Checkpoint 1]
Now generally, such a system can be designed in two ways.

Naive way

Naive UML Diagram

What's happening here? We've created a separate class for each of our shape and written the implementation of rotate() and playSound() methods 4 different times. Is it the best way to do things?

We are writing same piece of code 4 times instead what we should do is avoid this duplicity and reuse our code. Writing duplicate code is difficult to manage and is waste of time. If say I now want to change the implementation of rotate() function, I will have to change it at 4 places. But if I'm able to write only one implementation of my methods and somehow reuse it for all the shapes, that will be a better and much manageable code. Is it clear why doing things the naive way is not the best approach?
[Checkpoint 2]

Reusing our Code using Inheritance

Using inheritance for Code Reusability

What's happening here? You probably have guessed till now.

To make our code reusable, we are using the concept of inheritance in Java. As all 4 classes had the same implementation of rotate() and playSound(), we can say that this was common for all our 4 shapes.

What we've done here is introduced a new class Shape and moved the common methods to this class. We've inherited or extended our original 4 classes from this Shape class.

Whenever we now call rotate() method from say our Square class, it will call class Shape's implementation as Square is inherited from it.

This is Code Reusability at it's core. We are able to extract the common methods of our entities ad move them to a separate class and inherit our original entities from this newly created class so that we can reuse the methods and avoid code duplicity.

Are things clear till here? Till here we've shown what we wanted to achieve (Story 1), how we could've done it (Naive way), why it was not the best way (code duplicity) and how can we improve it (by using inheritance)
[Checkpoint 3]

Extending our Story

Now we want to add a new shape to our system, Rhombus, and rotate() method for rhombus will not have the same implementation as our previous 4 shapes. Instead it will have it's own implementation as the shape Rhombus needs to perform some extra logic in rotate(). The method playSound() on Rhombus will share the same implementation as previous classes. Is this new requirement clear?
[Checkpoint 4]

Method overriding

Adding a new Shape to our Design

Adding a new Shape to our design is that easy! All we have to do is create a new class with the name Rhombus and extend it from our Shape class created earlier. But is it enough for our new requirement?
No. Our requirement said that this Rhombus class will have it's own implementation of rotate() method which is different from the previous one.
What we'll do here is simply provide this new implementation of rotate() method in Rhombus class, it's that easy.

Overriding rotate() method

Here we say that Rhombus is extending Shape class, but will have his own implementation of rotate() method. In Java lingo, it's called method overriding.

It will mean that when rotate() is called on Rhombus, it's own implementation will be called rather than Shape class's implementation as Rhombus has overridden the rotate() method.

What about when playSound() is called on Rhombus? Implementation for this method is still coming from Shape class only, hence when playSound() is called on Rhombus, the implementation in Shape class will be called exactly as the requirement said.

Are things clear in this section? See how simply we were able to add a new shape to our design and give this shape it's own method implementation
[Checkpoint 5]

Takeaways

  • What is code reusability and why do we need it
  • How can we achieve it
  • What is method overriding

Acknowledgements

  • The class example is taken from Head First Java
comments powered by Disqus