Inheritance


  • Inheritance is the process by which one object acquires the properties of another object. This shows the hierarchical classification among objects. In life we consider many things related to each other in a hierarchical way. 
  • Each object in object oriented programming displays certain characteristics or properties of its own. If we don’t use hierarchical classifications we will have to explicitly define properties for each object in each class, which increases complexity. Even we will have to code each time for each object. This in turn will waste memory, time and efforts. 
  • Inheritance reduces complexity, and enhances programmer’s ability to write large or complex programs. This also enables us to reuse the code written for a particular class again and again.
  • For example, dog is a part of mammal class, which is turn is a part of carnivorous class, which is a part of larger class Animals. If want to describe animals; they can be distinguished by their size, intelligence and appearance.
  • They can be further classified by their physical abilities/characteristics, their habits. Now, if you observe animals have a common behavioral aspect; they eat, breathe, sleep and they reproduce. All animals respire, eat, sleep and reproduce.

Fig.Hierarchy of animals


                         So, we can grasp and learn things quickly by classification. Representation of information in a hierarchical way makes the information simple and easy to understand.
  • Another example of hierarchical would be classification of Biology, where biology is a study of living beings. 
  • It has two subclasses Botany and Zoology. Botany is a study of plants whereas Zoology is a study of animals. 
  • Now, plants have a common property i.e. they have leaves, they perform photosynthesis, they have branches, this all are under zoology. So we can study different species by classifying them and combining them together to form a larger class.
Fig.Hierarchy of Biology.
 

In inheritance the class that is inherited is called super class or base class whereas the class that inherits the property is called as subclass or derived class.
We can inherit a class by using extends keyword.
Previous
Next Post »