

This makes your code cleaner, understandable and extendable. Likewise, for the Footballer class, you inherit all the features of the Person class and add a new feature playFootball() and so on. So, for MathTeacher (derived class), you inherit all features of a Person (base class) and add a new feature teachMath(). Using inheritance, now you don't implement the same code for walk(), talk() and eat() for each class. It would be a lot easier if we had a Person class with basic features like talk, walk, eat, sleep, and add special skills to those features as per our characters. This can easily become error-prone (when copying) and duplicate codes. If you want to add a new feature - eat, you need to implement the same code for each character.

In each of the classes, you would be copying the same code to walk and talk for each character. You can individually create three classes who can walk, talk and perform their special skill. A math teacher can teach math, a footballer can play football and a businessman can run a business. However, they also have some special skills. Since, all of the characters are persons, they can walk and talk. Suppose, in your application, you want three characters - a math teacher, a footballer and a businessman. The derived class inherits all the features from the base class and can have additional features of its own.īefore going into details about Kotlin inheritance, we recommend you to check these two articles: It allows user to create a new class (derived class) from an existing class (base class). Inheritance is one of the key features of object-oriented programming.
