In this lesson, we will learn about Inheritance. Inheritance enables new classes to receive or inherit the properties and methods of existing classes.
is-A relationship
In the world around us, we are surrounded with entities that are connected to each other with "is-A" relationship. The concept of Inheritance helps us to model such "is-A" relationships in software. I know, right now you are a little confused with what "is-A" relationships I am talking about. So as always, lets look at some examples to develop more intuition about these "is-A" relationships and concretely understand Inheritance.
Take the example of traffic on the road, what this traffic consists of? There are Cars, Bikes, Scooters, Auto rickshaw, Buses, Trucks, etc. All of these have certain common characteristics and behaviour ā they can move on the road and transport people and goods from one place to another. These commonalities helps us to categorize them under one common category called Vehicles.
Now we can say that Car is a vehicle, Bus is a vehicle, Bike is a vehicle, Auto is a vehicle and so on. This is just one of the examples of "is-A" relationship around us. This helps us to conveniently say that there are many different types of vehicles on the road rather than calling out each of the type separately.
Building on it a little further, there are many different types of cars - i10, i20, XUV 500, Fortuner, Baleno, etc. This is also an example of is-A relationship where each one of these is-A car. Similarly for bikes, you may have a TVS Apache, Bajaj Pulsar, KTM Duke or Harley Davidson and each of them is a Bike. You can extend this similarly for buses and trucks as well. I hope this helps you to see how we can use is-A relationship to categorize entities based on common characteristics and behaviours. Also, most of the times these relationships are hierarchical ā there are vehicles then cars, bikes then the different models of these cars and bikes, etc.
You can look around and find other such examples as well like the different kind of animals - dog is an animal, cat is an animal, goat is an animal, elephant is an animal. Of course, you can make it more hierarchical like this
We have a base class Vertebrates for all the animals with a backbone. From there we can derive a mammals class to group animals that have mammary glands, give live birth to young ones, have 3 middle ear bones, etc. Mammal is-A vertebrate. Then from mammal we can derive specific examples of mammals like humans, whales, dog, Monkey, etc. Each one of these human, whale, dog, Monkey is-A mammal. This is another example of hierarchical is-A relationship where whale in addition to its own unique characteristics and behaviour, inherits all the characteristics and behaviour of mammals and vertebrates. Similarly, we can expand it for Birds, Reptiles, Fishes & Amphibians as well.
Inheritance helps in adapting to changes in software:
- It helps in avoiding code duplication. We can put the common code in a base class and share it with derived classes.
- It also reduces the need for code manipulation ā to implement a new functionality we can always create a new derived class rather than changing the code of the base class which may impact the entire application