OOP Principles: Abstraction OOP Principles: Abstraction


So, we have come to the last but very important principle of OOP: Abstraction. The word "abstraction" sounds quite abstract, and briefly, this principle tells us that we should depend on abstraction rather than on a specific implementation. However, this might sound complex now. Let's start by looking at the definition:

Abstraction is one of the principles of OOP that states that during the design of classes and creation of objects, it's necessary to highlight only the main properties of an entity and discard secondary ones.

For example, we have a class called Person, and many different classes are derived from it. Person has various fields and methods that are common to all classes. However, when we have a class called BasketballPlayer, the height characteristic becomes crucial, while for a class like Driver, height is irrelevant and not important. That's where abstraction comes into play. By inheriting from an abstraction, we can make the height parameter optional, so we don't have to use it where it's not necessary.

It might sound complex, so let's move on to an example:



The Car class doesn't represent something specific. There's no such thing as just a Car; there are sedans, trucks, tractors. Thus, we will depend on the abstraction called Car. To simplify it further: Car is a template based on which we create specific car classes.

Pay attention to the syntax:

This is the syntax for declaring an abstract class. Also, note the syntax for declaring an abstract method:

An abstract method doesn't have a body; this is it's main characteristic. Subclasses of an abstract class will override this abstract method to define their own implementation, using polymorphism.

Let's take a closer look at what an abstract class is:

An abstract class in Java is like a blueprint for other classes. It can't be instantiated itself, but it defines methods that must be implemented by its subclasses. It's a way to share common methods and enforce structure in related classes.

Note that we cannot create an object of an abstract class; it will result in an error:

Abstract classes free us from dealing with just "objects"; they provide us with a basic state and behavior. Taking the example of cars, each car should have a model, year of production, maximum speed, and color. They should also be able to move and stop. That's all; from there, you'll design your specific classes based on this abstract blueprint.

Rules for creating an abstract class

  1. An abstract class is a class that contains at least one abstract method. You can create an abstract class without any abstract methods, but then it wouldn't serve a purpose. You won't be able to instantiate objects of an abstract class;
  2. Abstract methods can only exist within an abstract class; you cannot create abstract methods outside of an abstract class;
  3. Methods within an abstract class can be either abstract methods or methods with implementations;
  4. A class that inherits from an abstract class must override all abstract methods;
  5. You can inherit from a maximum of one class (abstract or non-abstract);
  6. An abstract class should represent the common properties and behavior of its inheriting classes. We'll discuss this in more detail when we will study the SOLID principles.

Let's consider an example to understand how subclasses should override abstract methods by inheriting from an abstract class, using the example of the child class Truck:

Image 1
Image 2
Image 3

Let's take a closer look at what's written above in detail:

  • First slide: We are creating a subclass Truck that will inherit from the abstract class Car. We can see that the IDE prompts us to implement the required methods;
  • Second slide: We are overriding the necessary methods. For example, we will write that the Truck has started moving and then stopped;
  • Third slide: We create an object in the main method. Pay attention to the syntax used: Car name = new Truck();. We start creating the object through abstraction and end with a specific implementation. This is called composition. Then we assign values to the fields as needed and call the overridden methods.

Voilà! If you understand everything, you have learned how to use an abstract class!


Abstraction is probably the most challenging principle of OOP to understand, and it's quite likely that you haven't grasped everything at this moment. But don't think you're alone in this. Abstraction is a concept that initially proves difficult for most developers to grasp. Over time, you'll come to understand better how to work effectively with abstraction and abstract classes. Abstraction is a crucial principle because it allows us to make our programs extensible and highly flexible.

1. What is abstraction in Object-Oriented Programming?
2. Which of the following are true about abstraction?
3. What is the purpose of an abstract class in Java?
4. How does abstraction contribute to code maintainability?


What is abstraction in Object-Oriented Programming?

Select the correct answer


Which of the following are true about abstraction?

Select a few correct answers


What is the purpose of an abstract class in Java?

Select the correct answer


How does abstraction contribute to code maintainability?

Select the correct answer

Everything was clear?

Section 2. Chapter 7