INHERITANCE

Inheritance
Concept and Meaning of Inheritance:-

Inheritance is an important concept in object-oriented programming. It is the ability of a class of things to inherit the properties of its parent class. Such a class (daughter class) is usually called a derived class. Itis a way to form new classes using already defined It helps reuse existing code.

Inheritance is also known as generalization, because it establishes a hierarchical relationship between classes of objects. For instance, an "animal" is a generalization of "dogs", "cats", "cows" and others. Now since dog is an animal, it has all the characteristics of an animal such has it has 4 legs, a tail etc. Inheritance establishes a ‘is-a’ or ‘is-an’ relationship.

Inheritance helps reduce the complexity in a program as it eliminates the need to write the same code over and over again in a program.

Origin of Inheritance:-

The concept of inheritance and the term ‘object oriented programming’ was first introduced at Xerox PARC in the form of a language called Smalltalk. However, the concept of object oriented programming originated in the PDP-1 system at MIT. The language that used inheritance (and OOP) for the first time was Simula 67. This language was a favorite of Bjarne Stroutstrup, who later added its OOP features to C++, the language developed over the famous programming language, C.

Applications of inheritance:-

There are many different applications of inheritance. They are as follows:

1. Specialization

Inheritance is used to achieve what is called specialization. Specialization can be understood as the creation of a class based on existing classes or objects and adding only those features that are unique to the new class. This process is also known as subtyping. For example, a Student class may have have data like Name, Address, Birthdate etc. Now, a derived class of the Student class maybe a HigherSecondaryStudent class which can have data relating to chosen stream (such as commerce, science, arts etc.). Inheritance allows to create such derived classes without having to write the code pertaining to the data that is repeated throughout a program (as with name, address and birthdate in the above example).
2. Extension

Inheritacne is used to provide additional data or behavior features. This called extension or subclassing. As agianst specialization, with extension the new data or behaviors could have been provided in the inherited class because they are generally applicable to all instances of the class. Extension is used when adding new features into the inherited class is not possible or not appropriate.
3. Code re-use

As explained under specialization, inheritance helps in code re-use. It was one of the primary reasons for developing and using and is called implementation inheritance. Once some code has been written, it can be used again and again throughout a program using inheritance. This saves a large amount of time and energy on the part of the programmer.
4. Overriding

Another use of inheritance is overriding. Overriding is the ability of class or object to replace the implementation of an aspe or a behavior that it has inherited. Overriding aims at deciding which version of the behavior does code from the inherited class see. The answer varies between programming languages, and some languages provide the ability to indicate that a particular behavior is not to be overridden. For example, the Private, Public and Protected keywords used in C++ classes decide how the following data members and member functions are inherited. A private data member or a private member function cannot be accessed by any of its derived classes.


Example using C++

The following example shows how inheritance works.

Consider a class called ‘Shirts’which has Size, Color and Brand as its data members.
class Shirts
{
public:
int size;
char color[10], brand[20];
};

Now, let us consider two derived classes of the above Shirts class, namely, Printed_Shirts, Ordered_Shirts. Now, both of these classes will have the same data members as the original Shirts class. The only thing that makes them different is Print_Size (for Printed_Shirts) and Customer_Name (for Ordered Shirts). So, for this, we can use inheritance as follows.
class Printed_ Shirts:: public Shirts
{
public:
int print_ size;
};


class Ordered_ Shirts:: public Shirts
{
public:
char customer_name[30];
};

So, both the derived classes have only the unique data members defined for them while all other data members are inherited from the parent class, i.e., Shirts.

Post a Comment

Previous Post Next Post