Inheritance is one of the pillar of Object Oriented Paradigm. It is form of software reusability where new class(sub class) are created from already existing class (super class) which enhance the functionalities while using some of the properties from super class.
For example, Vehicle is a super class and Car, Plane, Motorcycle, Ship, etc are subclass of Vehicle. As you can see that sub class provides more specific functionality and clear picture of an object than super class.
Now lets undertand inheritance with code.
As you can see above, I have created Super class named Vehical. It has 3 properties, max speed, max passangers and ride on.
As you can see above, I have created Sub class named Car. It has only 1 property, model. But this class extends another class (Super class) Vehical. This is called Inheritance. Since Car class extends Vehical, Car class is called sub class and Vehical class is called Super Class.
Although Car class has only 1 property, it can still inherit properties of super class and use them or it can override super class properties. As you can see that, Car class inherit 3 properties of super class in constuctor with the use of keyword "Super".
As you can see above, I have created another Sub class named boat. It has only 1 property, making year. But again this class extends another class (Super class) Vehical.
As you can see above, I have created implementation class and trying to test it. As you can see, I created 2 objects of Car and Boat class. I am trying to print the properties of each object and the result is shown in the console. As you can see, that although sub class had only one property, it could inherit the properties of super class and override it with setters. If sub class do not override the properties of super class, it will get default value of super class. You will learn more about overriding in next chapters.
Along with inheriting properties of super class, sub class can also define its own properties. If you have noticed both sub class in the above example had its own property also name makeYear and model.
Hope, you have enjoyed this topic. If you have any question or comments please provide it below: