Use of Abstract Class in Object Oriented Design

 

 

Abstract classes and interfaces are one of the trickiest concepts to understand in Java. Syntactically, it’s pretty easy to understand and memorize the rules of Abstract class (or Interface). In this article, we will see not just implementation details but we will also try to understand the design level details of the concept.

Although this post is aimed at Java learners, it is suitable for learners of all programming languages. If you are seeking to learn just the concept, you will find this post immensely helpful.

Objectives of learning about Abstract Class

  • To understand the mechanics of an Abstract Class
  • To describe the need of abstract classes in an application

Meaning of the word: abstract

Wikipedia defines Abstract Art as:

departure from reality in depiction of imagery in art. This departure from accurate representation can be slight, partial, or complete”.

 

Meaning of abstract - Usage of Abstract Class

The wordabstract” essentially means something that is not real. When we say something is “abstract”, we say it is exists in thought or idea, but does not have a concrete implementation.

For example:. What is an Animal? Someone would say Deer or Lion; these are types of animals. All lions are animals, but all animals are not lions. If we think in terms of an Object, all lion objects have some concrete shape and size. Same goes with all Deer objects.

But what about the object “Animal”?. The word Animal  does not have any picture attached to it. Animal isn’t a real thing, it is always thought in terms of its sub-types i.e Cat, Dog, Lion, Wolf, etc.

When you ask someone to draw an Animal, they will most likely ask you back “Which Animal?”. Same thing goes with “Vehicle”.

These objects are incomplete on their own, we can say they are “abstract objects / entities”.  What completes them are their own sub types. So, we can conclude that:

 

Let’s again go back to the Animal example. The state of Animal object is undefined because we don’t know height, width, color, etc; Animal is abstract.

What is "Animal" - Usage of Abstract Class

What comes to your mind when you think of the word “Animal”?

However, Lion, Cat, Dog, Wolf – these objects have well-defined state i.e. these objects are concrete. There is an obvious relationship – Dog IS-A Animal, Cat IS-A Animal, and so on. Thereby we conclude that

What is an Abstract Method?

An abstract method is an incomplete method, it does not have a body – only a method signature which is followed by a semicolon at the end of statement. The word “abstract” is used to define an abstract method. For example,

An abstract method:

  1. Can have any return type or may not return anything.
  2. Can have any number of arguments of any type.
  3. CANNOT have a body, must be ended with a semi colon

In contrast, if a method has a body, it is called as a concrete method. Java being an object-oriented language, functions (whether abstract or concrete), have to be coded inside a class. Let us create two classes:

Here to call the printNew ( ) method, we will have to create an object of Beta and call the method on it.

 

When this method call gets executed, compiler branches to the memory area where the function is stored and starts executing from inside the method body.

When we do the same with class Alpha, the code will be:

Here what happens when you call the print ( ) method? Where does the compiler branch out for execution?

Effect of Abstract Methods - Usage of Abstract Class

 

By this, we can conclude that if a class has even one abstract method, the compiler will get stuck when trying to execute those incomplete methods. Hence, due to this undetermined behavior, the Java compiler forces you to declare a class with abstract methods as “abstract class”. The statement 1 in the second  listing above is invalid and will cause a compilation error.

What is an Abstract Class?

Just like a class is a blueprint of an object, an abstract class helps us capture the details of an abstract object. Abstract class is an incomplete without its sub-classes.

Abstract class cannot stand on its own. To use it, you have to create a concrete subclass.

This concrete subclass need not be at immediately the next level. If a super class has abstract methods, we can override and define them in a concrete sub class. Another option is to leave them undefined in our sub class to allow their definition somewhere further down the hierarchy. In that case, since subclass also has incomplete functionality, the sub class must declare itself as an abstract class.

Abstract Classes & Abstract Methods – Design Level Explanation

Since functionality is a part of an entity, an incomplete functionality makes the entity incomplete. For example, makeSound() function in Animal class. Each type of Animal will have a different sound. So makeSound() is abstract, that makes the Animal class abstract as well.

An entity can have many such functionalities – even one incomplete functionality makes the entire entity incomplete.

But an incomplete entity, does not necessarily mean that any of its functionalities are incomplete. It could also mean that whatever functionalities the entity is having currently they are complete, but some other functionalities are missing. THIS is because your entity is the parent.

Abstract Classes without Abstract Methods

Ab Abstract Class may contain zero, one, or more abstract methods

For example, a class Vehicle has methods forward(). All sub types of vehicle – Car, Bicycle, Motorcycle – can move forward. But a vehicle can also move reverse(), but only certain types of Vehicle (Car) can move naturally reverse on its own.

So even though ideally, a vehicle should be able to move forward and backward on its own, we can only include the forward() method, not all sub classes share the reverse() method in the same way.

Thus, the Vehicle class includes only forward() method, and the reverse() method ideally required but is missing from the class. This is a case of an entity with missing functionality. So, we make the Vehicle class abstract.

Rules of Abstract Methods

  1. An abstract method does not have a body; a semicolon terminates the method signature.
  2. Classes with even one method abstract, must be declared as “abstract”.
  3. Sub classes must provide an implementation of the abstract method, or must declare themselves as abstract

Rules of Abstract Classes

  1. A class is made abstract when its object has no realizable state. For example, “Animal” is not something that exists in real world, only DOG, CAT, or RABBIT can exist real world. So, “Animal” is a candidate for abstract class.
  2. An abstract class can contain both concrete as well as abstract methods.
  3. An abstract class may not contain any abstract methods at all.  

How can an Abstract class help me?

Abstract classes are powerful mechanisms to achieve Abstraction in our application. As we have learnt previously, abstraction helps us create a layer of simplicity among major modules of our application. This helps us in two ways:

  • By creating a layer of simplicity, abstraction enables us to hide unimportant details and expose only the relevant ones.
  • Because no module is concerned with details of other modules, developers can add enhancements to each of them separately without worrying about its effect on other dependencies. The end result is a loosely-coupled system.

Hope this explanation was enough for understanding Abstract Classes. If you still have any doubts, please put them in comments and I will answer them at the earliest.

Leave a Reply