Зміст курсу
Java OOP
Java OOP
OOP Principles: Abstraction
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:
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:
Car
public abstract class Car { String model; int year; int maxSpeed; String color; public abstract void move(); public abstract void stop(); }
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:
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
- 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;
- Abstract methods can only exist within an abstract class; you cannot create abstract methods outside of an abstract class;
- Methods within an abstract class can be either abstract methods or methods with implementations;
- A class that inherits from an abstract class must override all abstract methods;
- You can inherit from a maximum of one class (abstract or non-abstract);
- 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
:
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 classCar
. 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!
Summary
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.
Дякуємо за ваш відгук!