Hello! Today we will take a closer look at another principle of object-oriented programming (OOP) - inheritance. At the same time, we will study other types of relationships between classes - composition and aggregation. This topic will not be difficult: you have already encountered inheritance and its examples many times in previous lectures. Today the main thing will be to consolidate your knowledge, take a closer look at the mechanism of inheritance and once again go through the examples :) So, let's go!
Inheritance in Java and its benefits
As you probably remember, inheritance is a mechanism that allows you to describe a new class based on an existing (parent) one. In this case, the properties and functionality of the parent class are borrowed by the new class. Let's remember the example of inheritance from previous lectures:
public class Car {
private String model;
private int maxSpeed;
private int yearOfManufacture;
public Car(String model, int maxSpeed, int yearOfManufacture) {
this.model = model;
this.maxSpeed = maxSpeed;
this.yearOfManufacture = yearOfManufacture;
}
public void gas() {
//...gas
}
public void brake() {
//...brake
}
}
public class Truck extends Car {
public Truck(String model, int maxSpeed, int yearOfManufacture) {
super(model, maxSpeed, yearOfManufacture);
}
}
public class Sedan extends Car {
public Sedan(String model, int maxSpeed, int yearOfManufacture) {
super(model, maxSpeed, yearOfManufacture);
}
}
There is a certain program within which we work with various types of cars. Even if you are not a car enthusiast, you probably know that there are a great many types of these same cars in the world :) Therefore, we separate the general properties of cars into a common parent class - Car
. What do all cars have in common, regardless of type? Any car has a year of manufacture, model name and maximum speed. We put these properties into the fields model
, maxSpeed
, yearOfManufacture
. As for behavior, any car can accelerate and brake :) We define this behavior in methods gas()
and brake()
. What benefits does this give us? First of all, reducing the amount of code. Of course, we can do without the parent class. But since every car must be able to accelerate and brake, we will have to create methods gas()
in brake()
the class Truck
, in the class Sedan
, in the class F1Car
, in the class, Sportcar
and in all other classes of cars. Imagine how much extra code we will write in this case. Don’t forget about the model, maxSpeed and yearOfManufacture fields: if we abandon the parent class, we will create them in each of the machine classes! When we have a couple of dozen machine classes, the amount of repetitive code will become really serious. Moving common fields and methods (also called “state” and “behavior”) into the parent class will allow us to save a lot of time and space. If a certain type has properties or methods that are unique only to it and absent from other types of machines, it doesn’t matter. They can always be created in a descendant class, separately from all the others.
public class F1Car extends Car {
public void pitStop() {
//...only racing cars make pit stops
}
public static void main(String[] args) {
F1Car formula1Car = new F1Car();
formula1Car.gas();
formula1Car.pitStop();
formula1Car.brake();
}
}
Take the case of Formula 1 racing cars. They, unlike their “relatives”, have a unique behavior - from time to time they stop for a pit stop. This doesn't bother us. We have already described the general behavior in the parent class Car
, and we can add specific behavior of the descendant classes inside the classes. This also applies to fields: if a child class has unique properties, we can calmly declare these fields inside it and not worry :) The ability to reuse code is the main advantage of inheritance. It is very important for a programmer not to write unnecessary amounts of code. You will encounter this more than once in your work. Please remember one more very important thing: there is no multiple inheritance in Java. Each class inherits from only one class. We’ll talk about the reasons for this in more detail in future lectures, but for now just remember. This, by the way, distinguishes Java from some other OOP languages. For example, C++ has multiple inheritance. Everything is more or less clear with inheritance - let's move on.
Composition and Aggregation
Classes and objects can be related to each other. Inheritance describes the relationship "is" (or in English "IS A"). Leo is an Animal. This relationship can be easily expressed using inheritance, whereAnimal
the class will be the parent and Lion
the class will be the child. However, not all relationships in the world are described in this way. For example, a keyboard is definitely connected to a computer in some way, but it is not a computer . The hands are somehow connected with the person, but they are not the person. In these cases, it is based on a different type of relation: not “is”, but “is part” (“HAS A”). The hand is not a person, but it is part of a person. The keyboard is not a computer, but it is part of the computer. HAS A relationships can be described in code using composition and aggregation mechanisms . The difference between them lies in the “strictness” of these connections. Let's give a simple example: We have ours Car
- a car. Every car has an engine. In addition, every car has passengers inside. What is the fundamental difference between fields Engine engine
and Passenger [] passengers
? If a car has a passenger inside А
, this does not mean that there cannot be passengers in B
it C
. One car can accommodate several passengers. In addition, if all passengers are removed from the car, it will continue to function quietly. The relationship between class Car
and passenger mass Passenger [] passengers
is less strict. It's called aggregation . There is a good article on this topic: Relationships between classes (objects) . It provides another good example of aggregation. Let's say we have a class Student
denoting a student, and a class StudentsGroup
(a group of students). A student can be a member of a physics club, a Star Wars student fan club, or a KVN team. Composition is a more strict type of communication. When using composition, not only is an object part of an object, but it cannot belong to another object of the same type. The simplest example is a car engine. An engine is part of a car, but cannot be part of another car. As you can see, their connection is much stricter than that of Car
and Passengers
.
GO TO FULL VERSION