SlideShare a Scribd company logo
1 of 21
Inheritance
Ronak Chhajed
What is Inheritance ?
Inheritance is a relationship between two or more
classes where derived class inherits properties of pre-
existing (base) classes.
Base Class: It is the class whose properties are inherited
by another class. It is also called Super Class or Parent
Class.
Derived Class: It is the class that inherit properties from
base class(es). It is also called Sub Class or Child Class.
Concept of
Inheritance
Base Class
Property A
Property B
Property C
Property D
Property C
Property B
Property A
Derived
Class
Defined in
Derived Class
Defined in
Base Class
But accessible
from Derived
Class
Defining Derived Classes
A derived class can be defined by specifying its relationship
with the base class in addition to its own details.
The general form of defining a derived class is :
The colon indicates that the derived_class_name is derived
from the base_class_name.
The visibility mode (access specifier) is optional, if present,
may be private, public, or protected.
Class derived_class_name : visibility_mode base_class_name
{
………..// members of derived class
};
Visibility Mode
The visibility mode specifies how the features of the base
class are visible to the derived class.
Private : When a derived class privately inherits a base
class, the protected and public members of base class
become private members of the derived class.
Public : In Public mode, the protected and public members of
base class become protected and public members of derived
class respectively.
Protected : In Protected mode, the protected and public
members of base class become protected members of the
derived class.
Types of Inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Single Inheritance
In Single Inheritance, one derived class inherits from one
base class
Class A
Class B
Single Inheritance
class A
{
…….. // Members of base class
};
class B : public A
{
…….. // Members of derived class
};
Single Inheritance is declared as follows :
Multilevel Inheritance
In Multiple Inheritance, a class (Inter-mediate base class)
is derived from base class, which is again derived into
some other class
The class A serves as a
base class for the derived
class B, which in turn
serves as a base class
for the derived class C.
Class A
Class B
Class C
Multilevel Inheritance
class A
{
……… //Members of base class
{;
class B : public A
{
……… //Members of derived class, B derived from A
};
class C : public B
………. //Members of derived class, C derived from B
};
Multilevel Inheritance is declared as follows :
Multiple Inheritance
In Multiple Inheritance, a class is derived from more than
one base classes
Class C
Class BClass A
Base
Classes
Derived Class
Multiple Inheritance
class A
{
……… //Members of base class A
{;
class B
{
……… //Members of base class B
};
class C : public A, public B
{
………. //Members of derived class
};
Multiple Inheritance is defined as follows :
Hierarchical Inheritance
In Hierarchical Inheritance, one base class is inherited into
two or more Derived Classes
Class A
Class B Class C
Base Class
Derived Class
Hierarchical Inheritance
class A
{
……… //Members of base class A
{;
class B : public A
{
……… //Members of Derived class , B derived from A
};
class C : public A
{
………. //Members of derived class , C derived from A
};
Hierarchical Inheritance is declared as follows
Hybrid Inheritance
In Hybrid Inheritance, more than one type of inheritances
are used to derive a new Sub Class.
For example, inheriting a class from two different classes,
which in turn have been derived from the same base class.
Any legal combination of other four types of inheritance
comes under Hybrid Inheritance
Hybrid Inheritance
Class A
Class B Class C
Base Class
Class D Derived Class
Intermediate
Base Class
Hybrid Inheritance
class A
{
………..
………..
};
class B : public A
{
……….
};
class C : public A
{
………..
};
class D : public B, public C
{
………
};
A basic form of Hybrid Inheritance :
Virtual Base Class
Virtual base class is a way of preventing multiple
instances(duplicate copies) of a given class appearing in
an inheritance hierarchy when using multiple/hybrid
inheritance.
Suppose we have a base class called ‘A’ and two classes
derived from it ‘B’ and ‘C’ and we derived class ‘D’ from
class ‘B’ and ‘C’.
That means all the members of class ‘A’ are inherited into
‘D’ twice, first via ‘B’ and again via ‘C’ This means, class
‘D’ would have duplicate sets of members inherited from
‘A’. This is an ambiguous situation
Virtual Base Class
class A
{
………..
………..
};
class B : virtual public A
{
……….
};
class C : virtual public A
{
………..
};
class D : public B, public C
{
………
};
This is how we create Virtual Base Class
Abstract Class
An abstract class is one that is not used to create
objects.
An abstract class is designed only to act as a base class
to be inherited by other classes.
It is a design concept in program development and
provides a base upon which other classes may be built
Advantages of Inheritance
Reusability : Inheritance helps the code to be reused in
many situations. The base class is defines and once it is
compiled, it need not be reworked. Using the concept of
Inheritance, the programmer can create as many derived
classes from the base class as needed while adding
specific features to each derived class as needed
Saves Time and Effort : The above concept of reusability
achieved by inheritance saves the programmer’s time
and effort. Since the main code written can be reused in
various situations as needed

More Related Content

What's hot

friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS abhishek kumar
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingAshita Agrawal
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaMadishetty Prathibha
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 

What's hot (20)

Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Inheritance
InheritanceInheritance
Inheritance
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Interface in java
Interface in javaInterface in java
Interface in java
 
inheritance
inheritanceinheritance
inheritance
 
OOP java
OOP javaOOP java
OOP java
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 

Similar to Inheritance in OOPS

E -COMMERCE.ppt
E -COMMERCE.pptE -COMMERCE.ppt
E -COMMERCE.pptclassall
 
Ritik (inheritance.cpp)
Ritik (inheritance.cpp)Ritik (inheritance.cpp)
Ritik (inheritance.cpp)RitikAhlawat1
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdfWaqarRaj1
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optdeepakskb2013
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerssuser6f3c8a
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)Redwan Islam
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxurvashipundir04
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 
week14 (1).ppt
week14 (1).pptweek14 (1).ppt
week14 (1).pptamal68766
 
oop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfoop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfitxminahil29
 

Similar to Inheritance in OOPS (20)

Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritence
inheritenceinheritence
inheritence
 
iheritence.pptx
iheritence.pptxiheritence.pptx
iheritence.pptx
 
E -COMMERCE.ppt
E -COMMERCE.pptE -COMMERCE.ppt
E -COMMERCE.ppt
 
Ritik (inheritance.cpp)
Ritik (inheritance.cpp)Ritik (inheritance.cpp)
Ritik (inheritance.cpp)
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Single inheritance
Single inheritanceSingle inheritance
Single inheritance
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ opt
 
Inheritance
InheritanceInheritance
Inheritance
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
week14 (1).ppt
week14 (1).pptweek14 (1).ppt
week14 (1).ppt
 
oop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfoop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdf
 

Recently uploaded

ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456KiaraTiradoMicha
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 

Recently uploaded (20)

ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

Inheritance in OOPS

  • 2. What is Inheritance ? Inheritance is a relationship between two or more classes where derived class inherits properties of pre- existing (base) classes. Base Class: It is the class whose properties are inherited by another class. It is also called Super Class or Parent Class. Derived Class: It is the class that inherit properties from base class(es). It is also called Sub Class or Child Class.
  • 3. Concept of Inheritance Base Class Property A Property B Property C Property D Property C Property B Property A Derived Class Defined in Derived Class Defined in Base Class But accessible from Derived Class
  • 4. Defining Derived Classes A derived class can be defined by specifying its relationship with the base class in addition to its own details. The general form of defining a derived class is : The colon indicates that the derived_class_name is derived from the base_class_name. The visibility mode (access specifier) is optional, if present, may be private, public, or protected. Class derived_class_name : visibility_mode base_class_name { ………..// members of derived class };
  • 5. Visibility Mode The visibility mode specifies how the features of the base class are visible to the derived class. Private : When a derived class privately inherits a base class, the protected and public members of base class become private members of the derived class. Public : In Public mode, the protected and public members of base class become protected and public members of derived class respectively. Protected : In Protected mode, the protected and public members of base class become protected members of the derived class.
  • 6. Types of Inheritance 1. Single Inheritance 2. Multilevel Inheritance 3. Multiple Inheritance 4. Hierarchical Inheritance 5. Hybrid Inheritance
  • 7. Single Inheritance In Single Inheritance, one derived class inherits from one base class Class A Class B
  • 8. Single Inheritance class A { …….. // Members of base class }; class B : public A { …….. // Members of derived class }; Single Inheritance is declared as follows :
  • 9. Multilevel Inheritance In Multiple Inheritance, a class (Inter-mediate base class) is derived from base class, which is again derived into some other class The class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. Class A Class B Class C
  • 10. Multilevel Inheritance class A { ……… //Members of base class {; class B : public A { ……… //Members of derived class, B derived from A }; class C : public B ………. //Members of derived class, C derived from B }; Multilevel Inheritance is declared as follows :
  • 11. Multiple Inheritance In Multiple Inheritance, a class is derived from more than one base classes Class C Class BClass A Base Classes Derived Class
  • 12. Multiple Inheritance class A { ……… //Members of base class A {; class B { ……… //Members of base class B }; class C : public A, public B { ………. //Members of derived class }; Multiple Inheritance is defined as follows :
  • 13. Hierarchical Inheritance In Hierarchical Inheritance, one base class is inherited into two or more Derived Classes Class A Class B Class C Base Class Derived Class
  • 14. Hierarchical Inheritance class A { ……… //Members of base class A {; class B : public A { ……… //Members of Derived class , B derived from A }; class C : public A { ………. //Members of derived class , C derived from A }; Hierarchical Inheritance is declared as follows
  • 15. Hybrid Inheritance In Hybrid Inheritance, more than one type of inheritances are used to derive a new Sub Class. For example, inheriting a class from two different classes, which in turn have been derived from the same base class. Any legal combination of other four types of inheritance comes under Hybrid Inheritance
  • 16. Hybrid Inheritance Class A Class B Class C Base Class Class D Derived Class Intermediate Base Class
  • 17. Hybrid Inheritance class A { ……….. ……….. }; class B : public A { ………. }; class C : public A { ……….. }; class D : public B, public C { ……… }; A basic form of Hybrid Inheritance :
  • 18. Virtual Base Class Virtual base class is a way of preventing multiple instances(duplicate copies) of a given class appearing in an inheritance hierarchy when using multiple/hybrid inheritance. Suppose we have a base class called ‘A’ and two classes derived from it ‘B’ and ‘C’ and we derived class ‘D’ from class ‘B’ and ‘C’. That means all the members of class ‘A’ are inherited into ‘D’ twice, first via ‘B’ and again via ‘C’ This means, class ‘D’ would have duplicate sets of members inherited from ‘A’. This is an ambiguous situation
  • 19. Virtual Base Class class A { ……….. ……….. }; class B : virtual public A { ………. }; class C : virtual public A { ……….. }; class D : public B, public C { ……… }; This is how we create Virtual Base Class
  • 20. Abstract Class An abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class to be inherited by other classes. It is a design concept in program development and provides a base upon which other classes may be built
  • 21. Advantages of Inheritance Reusability : Inheritance helps the code to be reused in many situations. The base class is defines and once it is compiled, it need not be reworked. Using the concept of Inheritance, the programmer can create as many derived classes from the base class as needed while adding specific features to each derived class as needed Saves Time and Effort : The above concept of reusability achieved by inheritance saves the programmer’s time and effort. Since the main code written can be reused in various situations as needed

Editor's Notes

  1. This is just opposite of Multiple Inheritance
  2. This is a combine form of Multiple and Hierarchical Inheritance.