CONCEPTS OF
OBJECT ORIENTED
PROGRAMMING
UNIT-1
1.2 Inheritance
Inheritance
• The capability of a class to derive properties and characteristics from
another class is called Inheritance.
• Inheritance is one of the most important features of Object-Oriented
Programming.
• Inheritance is a feature or a process in which, new classes are created
from the existing classes.
• The new class created is called “derived class” or “child class” and the
existing class is known as the “base class” or “parent class”.
• The derived class now is said to be inherited from the base class.
• It means, the derived class inherits all the properties of the base class,
without changing the properties of base class and may add new
features to its own.
• These new features in the derived class will not affect the base class.
The derived class is the specialized class for the base class.
• Sub Class: The class that inherits properties from another class is
called Subclass or Derived Class.
• Super Class: The class whose properties are inherited by a subclass is
called Base Class or Superclass.
Implementing inheritance in C++:
• For creating a sub-class that is inherited from the base class we have to follow the below syntax.
• Derived Classes: A Derived class is defined as the class derived from the base class.
Syntax:
• class <derived_class_name> : <access-specifier> <base_class_name>
{
//body
}
Where
class — keyword to create a new class
derived_class_name — name of the new class, which will inherit the base class
access-specifier — either of private, public or protected. If neither is specified, PRIVATE is taken as default
base-class-name — name of the base class
Note: A derived class doesn’t inherit access to private data members. However, it does inherit a full parent object,
which contains any private members which that class declares.
Types
• Single inheritance
• Multilevel inheritance
• Multiple inheritance
• Hierarchical inheritance
• Hybrid inheritance
Single Inheritance:
In single inheritance, a class is allowed to inherit from only one class. i.e. one subclass is
inherited by one base class only.
• Syntax:
class subclass_name : access_mode base_class
{
// body of subclass
};
OR
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
Multiple Inheritance:
It is a feature of C++ where a class can inherit from more than one class. i.e one subclass is inherited from more
than one base class.
Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2, ....
{
// body of subclass
};
class B
{
... .. ...
};
class C
{
... .. ...
};
class A: public B, public C
{
... ... ...
};
Multilevel Inheritance:
In this type of inheritance, a derived class is created from another derived
class.
Syntax: -
class C
{
... .. ...
};
class B:public C
{
... .. ...
};
class A: public B
{
... ... ...
};
Hierarchical Inheritance:
In this type of inheritance, more than one subclass is inherited from a single base class. i.e.
more than one derived class is created from a single base class.
Syntax:-
class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D : public A
{
// body of class D.
}
Hybrid (Virtual) Inheritance:
Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and
Multiple Inheritance.