Inheritance (1)
Inheritance (1)
What Is Inheritance?
• Inherit Definition -
Derive quality and characteristics from parents or
ancestors. Like you inherit features of your
parents.
• Example:
"She had inherited the beauty of her mother"
• Inheritance in Object Oriented Programming can
be described as a process of creating new classes
from existing classes.
• New classes inherit some of the properties
and behaviour of the existing classes. An
existing class that is "parent" of a new class is
called a base class. New class that inherits
properties of the base class is called a derived
class(“child class”).
• Inheritance is a technique of code reuse. It
also provides possibility to extend existing
classes by creating derived classes.
The "is a" Relationship
• Inheritance establishes an "is a" relationship
between classes.
– A poodle is a dog
– A car is a vehicle
– A flower is a plant
– A football player is an athlete
Types of Inheritance
• Single Inheritance: It is the inheritance hierarchy
wherein one derived class inherits from one base class.
• Multiple Inheritance: It is the inheritance hierarchy
wherein one derived class inherits from multiple base
class(es).
• Hierarchical Inheritance: It is the inheritance hierarchy
wherein multiple subclasses inherit from one base
class.
• Multilevel Inheritance: It is the inheritance hierarchy
wherein subclass acts as a base class for other classes.
• Hybrid Inheritance: The inheritance hierarchy that
reflects any legal combination of other four types of
inheritance.
Access specifier
Same class ✔ ✔ ✔
Derived ✔ ✔ ❌
classes
Outside ✔ ❌ ❌
classes
Protected Members and
Class Access
• protected member access specification:
like private, but accessible by objects of
derived class
Inheritance – Terminology and
Notation in C++
• Base class (or parent) – inherited from
• Derived class (or child) – inherits from the base class
• Notation:
class Base // base class
{
. . .
};
class Derived : modeofinheritance Base
{ // derived class
. . .
};
modeofinheritance can be public, private and
protected.
Mode of Inheritace
• Mode of Inheritance: determines how private,
protected, and public members of base class
are inherited by the derived class
Mode of Inheritance
How inherited base class
members
Base class members appear in derived class
private: x private x is inaccessible
protected: y base class
private: y
public: z private: z