You are on page 1of 15

INHERITANCE

MADE BY: VEDANT SINGH


BCA IV ‘B’
BC2017110
INHERITANCE
• The capability of a class to derive properties and characteristics from another
class is called Inheritance.
• Sub Class: The class that inherits properties from another class is called Sub
class or Derived Class.
• Super Class: The class whose properties are inherited by sub class is called
Base Class or Super class.
USE OF INHERITANCE
Let us consider three classes: bus, car and bike.
Class Bus Class Car Class Bike
FuelAmount() FuelAmount() FuelAmount()
Mileage() Mileage() Mileage()
ApplyBrakes() ApplyBrakes() ApplyBrakes()
Instead, let’s create a class Automobile.

Class Automobile

FuelAmount()
Mileage()
ApplyBrakes()

Class Bus Class Car Class Bike


• This decreases the chances of error.
• This decreases data redundancy.
• Provides reusability.
• This reduces the length of the program.
SYNTAX
class subclass_name : access_mode base_class_name
{
// body of subclass
};
MODES OF INHERITANCE
• Public mode: If we derive a sub class from a public base class, then the public
member of the base class will become public in the derived class and
protected members of the base class will become protected in derived class.
• Protected mode: If we derive a sub class from a protected base class, then
both public member and protected members of the base class will become
protected in derived class.
• Private mode: If we derive a sub class from a private base class, then both
public member and protected members of the base class will become private
in derived class.
class A class C : protected A
{ public: { // x is protected
int x; // y is protected
protected: // z is not accessible from C
int y; };
private:
int z; class D: private A
}; { // x is private
class B : public A // y is protected
{ // x is public // z is not accessible from D
// y is protected };
// z is not accessible from B
};
TYPES OF INHERITANCE
• Single Inheritance: A class is allowed to inherit from only one class,
i.e. one sub class is inherited by one base class only.
Syntax:
class subclass_name : access_mode base_class Class A (Base Class)
{
//body of subclass
}; Class B (Derived Class)
• Multiple Inheritance: A class can inherit from more than one classes, i.e.
one sub class is inherited from more than one base classes.
Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2, …

(Base Class 1) Class A Class B (Base Class 2)

Class C (Derived Class)


• Multilevel Inheritance: A derived class is created from another
derived class.
Class A (Base Class 2)

Class B (Base Class 1)

Class C
(Derived Class)
• Hierarchical Inheritance: More than one sub class is
inherited from a single base class, i.e. more than one derived
class is created from a single base class.

Class A

Class A Class A

Class A Class A Class A Class A

You might also like