What is "inheritance" in OOP
Inheritance is a property of the system that allows you to describe a new class based on an existing one with partially or completely borrowed functionality.
The class from which inheritance is made is called ancestor, base or parent. The new class is a descendant, inheritor, or derived class.
Let's imagine ourselves, for a moment, engineers of an automobile plant. Our task is to develop a modern car. We already have a previous model that has proven itself over the years. Everything would be fine, but times and technologies are changing, and our modern plant should strive to improve the convenience and comfort of products and meet modern standards.
We need to make a whole range of cars: sedan, station wagon and subcompact hatchback. Obviously, we are not going to design a new car from scratch, but, taking the previous generation as a basis, we will make a number of design changes. For example, let's add power steering and reduce the gaps between the fenders and the hood lid, and install fog lights. In addition, the body shape will be changed in each model.
Obviously, all three modifications will have most of the properties of the previous model (good old engine of 1970, impenetrable chassis, which has proven itself in an excellent way on domestic roads, gearbox, etc.). Moreover, each of the models will implement some new functionality or design feature. In this case, we are dealing with inheritance.
Example: Consider an example of creating a smartphone class using inheritance. All cordless telephones operate on rechargeable batteries, which have a certain operating life in hours. So let's add this property to the cordless phone class:
public abstract class CordlessPhone extends AbstractPhone {
private int hour;
public CordlessPhone(int year, int hour) {
super(year);
this.hour = hour;
}
}
Cell phones inherit the properties of the cordless phone, we also added call and ring methods to this class:
public class CellPhone extends CordlessPhone {
public CellPhone(int year, int hour) {
super(year, hour);
}
@Override
public void call(int outputNumber) {
System.out.println("Calling number " + outputNumber);
}
@Override
public void ring(int inputNumber) {
System.out.println("The subscriber is calling you " + inputNumber);
}
}
And, finally, the smartphone class, which, unlike classic cell phones, has a full-fledged operating system. You can add new programs supported by this operating system to your smartphone, thus expanding its functionality. Using code, the class can be described as follows:
public class Smartphone extends CellPhone {
private String operationSystem;
public Smartphone(int year, int hour, String operationSystem) {
super(year, hour);
this.operationSystem = operationSystem;
}
public void install(String program){
System.out.println("Installing " + program + "for" + operationSystem);
}
}
As you can see, we created quite a bit of new code to describe the Smartphone class, but we got a new class with new functionality. Using this principle of OOP java allows you to significantly reduce the amount of code, and therefore, to facilitate the work of the programmer.
Read also:
Comments
Post a Comment