The document discusses inheritance in object-oriented programming, which allows a subclass to inherit attributes and behaviors from a parent superclass, creating an "is-a" relationship. Inheritance enables code reuse as subclasses inherit functionality from the superclass and can add additional specialized attributes and behaviors. The different types of inheritance covered are single, multilevel, multiple, and hybrid inheritance, along with access specifiers like public, private, and protected that determine which superclass members are inherited by subclasses.
3. 10-3
What is Inheritance?
Generalization vs. Specialization
• Real-life objects are typically specialized versions of
other more general objects.
• The term “insect” describes a very general type of
creature with numerous characteristics.
• Grasshoppers and bumblebees are insects
– They share the general characteristics of an insect.
– However, they have special characteristics of their own.
• grasshoppers have a jumping ability, and
• bumblebees have a stinger.
• Grasshoppers and bumblebees are specialized
versions of an insect.
5. 10-5
The “is a” Relationship
• The relationship between a superclass and an
inherited class is called an “is a” relationship.
– A grasshopper “is a” insect.
– A poodle “is a” dog.
– A car “is a” vehicle.
• A specialized object has:
– all of the characteristics of the general object, plus
– additional characteristics that make it special.
• In object-oriented programming, inheritance is used
to create an “is a” relationship among classes.
6. 10-6
The “is a” Relationship
• We can extend the capabilities of a class.
• Inheritance involves a superclass and a subclass.
– The superclass is the general class and
– the subclass is the specialized class.
• The subclass is based on, or extended from, the superclass.
– Superclasses are also called base classes, and
– subclasses are also called derived classes.
• The relationship of classes can be thought of as parent
classes and child classes.
7. 10-7
Inheritance, Fields and Methods
• Members of the superclass that are marked private:
– are not inherited by the subclass,
– exist in memory when the object of the subclass is created
– may only be accessed from the subclass by public methods
of the superclass.
• Members of the superclass that are marked public:
– are inherited by the subclass, and
– may be directly accessed from the subclass.
8. Inheritance
• It is the Process of creating a New class from an existing class.
• The Existing class is called Base or Parent class.
• The New class is called as Child or Derived Class
base
subclass1 subclass2
9. Advantages
It permits code reusability. So, save time and
increase the program reliability.
Improve Program Reliability
It Permits code sharing
11. Access specifier
• Public - The members declared as Public are accessible
from outside the Class through an object of the class.
• Protected - The members declared as Protected are
accessible from outside the class BUT only in a class
derived from it.
• Private - These members are only accessible from
within the class. No outside Access is allowed.
26. It is the process of creating new class from more than one base
classes.
Syntax :
class <derived class >:<access specifier>
base_class1,<access specifier> base_class2...
{
private :
// members;
protected :
// members;
public :
//memebers;
};
Multiple Inheritance
Person Employee
Teacher
44. Order of Constructor Call
Base class constructors are always called in the derived class constructors.
Whenever you create derived class object, first the base class default
constructor is executed and then the derived class's constructor finishes
execution.
Points to Remember
1.Whether derived class's default constructor is called or parameterised is
called, base class's default constructor is always called inside them.
2.To call base class's parameterised constructor inside derived class's
parameterised constructor, we must mention it explicitly while declaring
derived class's parameterized constructor.
45.
46.
47. Base class Parameterized Constructor in Derived class Constructor
We can explicitly mention to call the Base class's parameterized constructor
when Derived class's parameterized constructor is called.
48.
49. Constructor call in Multiple Inheritance
Its almost the same, all the Base class's constructors are called inside derived class's
constructor, in the same order in which they are inherited.
class A : public B, public C ;
In this case, first class B constructor will be calledd, then class C constructor and then
class A constructor.
50. Function Type
Access directly to
Private Protected Public
Class member Yes Yes Yes
Derived class
member
No Yes Yes
Friend Yes Yes Yes
Friend class
member
Yes Yes Yes
51. Public Inheritance:
All Public members of the Base Class become Public Members of the derived class &
All Protected members of the Base Class become Protected Members of the Derived
Class.
class Base {
public: int a;
protected: int b;
private: int c;
};
class Derived:public Base {
void doSomething() {
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error
}
};
int main() {
Derived obj;
obj.a = 10; //Allowed
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error
}
52.
53. Private Inheritance:
All Public members of the Base Class become Private Members of the Derived class &
All Protected members of the Base Class become Private Members of the Derived Class.
Class Base {
public: int a;
protected: int b;
private: int c; };
class Derived:private Base //defaults access specifier is private {
void doSomething() {
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error }
};
class Derived2:public Derived {
void doSomethingMore() {
a = 10; //Not Allowed, Compiler Error, a is private member of Derived now
b = 20; //Not Allowed, Compiler Error, b is private member of Derived now
c = 30; //Not Allowed, Compiler Error }
};
int main() {
Derived obj; obj.a = 10; //Not Allowed, Compiler Error
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error }
54. • Protected Inheritance:
All Public members of the Base Class become Protected Members of the derived
class & All Protected members of the Base Class become Protected Members of the
Derived Class.
Class Base {
public: int a;
protected: int b;
private: int c; };
class Derived:protected Base {
void doSomething() {
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error }
};
class Derived2:public Derived {
void doSomethingMore() {
a = 10; //Allowed, a is protected member inside Derived & Derived2 is public
derivation from Derived, a is now protected member of Derived2
b = 20; //Allowed, b is protected member inside Derived & Derived2 is public
derivation from Derived, b is now protected member of Derived2
c = 30; //Not Allow ed, Compiler Error }
};
56. Access in Base
Class
Access in
Publically
Derived Class
Access in
Privately
Derived Class
Access in
Protected
Derived Class
Public Public Private Protected
Private
Cannot be
accessed
directly
(only base
inherited class
member
functions can
access)
Cannot be
accessed
directly
(only base
inherited class
member
functions can
access)
Cannot be
accessed
directly
(only base
inherited class
member
functions can
access)
Protected Protected Private Protected
57. Case study – Railway reservation system
Single Inheritance