SlideShare a Scribd company logo
INHERITENCE
SUBMITTED BY:
R.RAMADEVI
M.Sc(CS&IT)
NSCAS
SYNOPSIS
 INHERITANCE DEFINITION
 SYNTAX INACCESS SPECIFIERS
 PUBLIC INHERITANCE
 PROTECTED INHERITANCE
 PRIVATE INHERITANCE
 TYPES OF INHERITANCE
 SINGLE INHERITANCE
 MULIPLE INHERITANCE
 MULITLEVE INHERITANCE
 HIERARCHICAL INHERITANCE
 HYBRID INHERITANCE
 DIAMOND INHERITANCE
INHERITANCE DEFINITION
• Derived quality and characteristics from parents or ancestors
• Like you inherit features of your parents
For example :
“she had inherited the beauty of her Mother”
• Inheritance in object oriented programming can be described as a process of
creating new class from exiting classes
• New classes inheritance some of the properties and behavior 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
• Inheritance is a technique of code reuse
• It also provides possibility to extend existing classes by creating derived
classes
SYNTAX
• The basic syntax of inheritance is:
class derived class : access specified base class
• Access specified can be public , protected and private
• The default access specified is private
• Access specified affect accessibility of data member of bass class from
the derived class
INHERITANCE ACCESS SPECIFIERS
PUBLIC INHERITANCE
• This inheritance mode is used mostly
• In this the protected member of base class becomes protected members of
derived class and public become public
• Drived class of derived classes:
• If we are inheriting a derived class using a public inheriting as shows below
class B:public A
class C:public B
• Then public and protected members of class A will be accessible in class
C as public and protected respectively
PROTECTED INHERITANCE
• In protected mode the public and protected members of base class
becomes protected members of derived class
Example:
class derived class: protected base class
• If we are inheritance a derived class using a public inheritance as shown
below
class B:protected A
class C:protected B
• Then public and protected member of class
• A will be accessible in class c as protected
PRIVATE INHERITENCE
• In private mode the public and protected member of base class become private members
of derived class
FOR EXAMPLE:
class derived class : private base class
class derived class : base class
• If we are inheriting a derived class using a public inheritance as shows below
FOR EXAMPLE:
class B : private A
class C : private B
TYPES OF INHERITANCE
• There are different type of inheritance
 Single inheritance
Multiple inheritance
Multilevel inheritance
Hierarchical inheritance
Hybrid(virtual) inheritance
SINGLE INHERITANCE
• Single inheritance represents a from of inheritance when there is only one
base class and one derived class
FOR EXAMPLE:
A class describes a person
SINGLE INHERITANCE PROGRAM
• We want to create new class student which should have the same
information as person class plus one new information about uninersity
• In this case we can create a derived should have the same information as
person class plus one new information about university
• we can create a derived class student
• If you want to call the parameterized(user defined)
• Constructor of a base class from a derived class then you need to write a
parameterized constructor of a derived class as below
• We can access member function of base class from a derived class
FOR EXAMPLE:
• We can create a new print() function in a derived class that uses printf()
member function of a base class
MULTIPLE INHERITANCE
Multiple inheritance represents a kind of inheritance when a derived class
inherits properties of multiple classes
FOR EXAMPLE:
• There are three classes A ,Band C and derived class is D as shows below
Class A Class B Class c
Class D
MULTIPLE INHERITANCE PROGRAM
• class derived class : access specified
• base class 1.base class 2..base class n
• You can create a new class that will
• Inherit all the properties of all these
class
SYNTAX
MULTILEVEL INHERITANCE
 Multilevel inheritance represents a type of inheritance when a derived class
is a base class for another class
 In other words deriving a class from a derived class is know as multilevel
inheritance
 Simple multilevel inheritance is shown in below image
 Where class A is a parent of class B and class B is a parent of class C
MULILEVEL INHERITANCE EXAMPLE
• As you can see class person is the base class of both
student employee classes
• At the same time class student is the basic
class for IT student and math student classes
• Employee is the base class for derived and
engineer classes
HIERARCHICAL INHERTANCE
• When there is a need to create multiple derived classes that inheritance
properties of the same base class is known as hierarchical inheritance
EXAMPLE PROGRAM
HYBRID INHERITANCE
• Combination of multilevel and hierarchical inheritance will give you hybrid
inheritance
CLASS A
CLASS B CLASS C
CLASS D
DIAMOND PROBLEM
• When you have a hybrid inheritance then a diamond problem may arise
• In this problem a derived class will have multiple paths to a base
• This will result in duplicate inheritance member of the base class
• This kind of problem is known as diamond problem
DIAMOND DIAGRAM
GRANDPARENT
CHILD 1 CHILD 2
GRANDSONS
VIRTUAL INHERITANCE
Child classes in this case should
inheritance grandparent class by
using Virtual inheritance
THANK YOU!!!

More Related Content

Similar to Inheritance

INHERITANCES.pptx
INHERITANCES.pptxINHERITANCES.pptx
INHERITANCES.pptx
VISHNU PRIYA NR
 
2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance
kinan keshkeh
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
Arslan Waseem
 
Inheritance slides
Inheritance slidesInheritance slides
Inheritance slides
awaisch888
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
baabtra.com - No. 1 supplier of quality freshers
 
inheritance in C++ programming Language.pptx
inheritance in C++ programming Language.pptxinheritance in C++ programming Language.pptx
inheritance in C++ programming Language.pptx
rebin5725
 
Object oriented programming new syllabus presentation
Object oriented programming new syllabus presentationObject oriented programming new syllabus presentation
Object oriented programming new syllabus presentation
iqraamjad1405
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
WaqarRaj1
 
inheritence
inheritenceinheritence
inheritence
Adil Memon
 
Ayan Das_25300121057.pptx
Ayan Das_25300121057.pptxAyan Das_25300121057.pptx
Ayan Das_25300121057.pptx
Ayan974999
 
Inheritance
InheritanceInheritance
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
AteeqaKokab1
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
LadallaRajKumar
 
Inheritance
InheritanceInheritance
Inheritance
rajshreemuthiah
 

Similar to Inheritance (20)

INHERITANCES.pptx
INHERITANCES.pptxINHERITANCES.pptx
INHERITANCES.pptx
 
2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Inheritance slides
Inheritance slidesInheritance slides
Inheritance slides
 
Oops
OopsOops
Oops
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
inheritance in C++ programming Language.pptx
inheritance in C++ programming Language.pptxinheritance in C++ programming Language.pptx
inheritance in C++ programming Language.pptx
 
Object oriented programming new syllabus presentation
Object oriented programming new syllabus presentationObject oriented programming new syllabus presentation
Object oriented programming new syllabus presentation
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
inheritence
inheritenceinheritence
inheritence
 
Ayan Das_25300121057.pptx
Ayan Das_25300121057.pptxAyan Das_25300121057.pptx
Ayan Das_25300121057.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
Java
JavaJava
Java
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 

More from lalithambiga kamaraj

Firewall in Network Security
Firewall in Network SecurityFirewall in Network Security
Firewall in Network Security
lalithambiga kamaraj
 
Data Compression in Multimedia
Data Compression in MultimediaData Compression in Multimedia
Data Compression in Multimedia
lalithambiga kamaraj
 
Data CompressionMultimedia
Data CompressionMultimediaData CompressionMultimedia
Data CompressionMultimedia
lalithambiga kamaraj
 
Digital Audio in Multimedia
Digital Audio in MultimediaDigital Audio in Multimedia
Digital Audio in Multimedia
lalithambiga kamaraj
 
Network Security: Physical security
Network Security: Physical security Network Security: Physical security
Network Security: Physical security
lalithambiga kamaraj
 
Graphs in Data Structure
Graphs in Data StructureGraphs in Data Structure
Graphs in Data Structure
lalithambiga kamaraj
 
Package in Java
Package in JavaPackage in Java
Package in Java
lalithambiga kamaraj
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
lalithambiga kamaraj
 
Data structure
Data structureData structure
Data structure
lalithambiga kamaraj
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
lalithambiga kamaraj
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
lalithambiga kamaraj
 
Estimating Software Maintenance Costs
Estimating Software Maintenance CostsEstimating Software Maintenance Costs
Estimating Software Maintenance Costs
lalithambiga kamaraj
 
Datamining
DataminingDatamining
Digital Components
Digital ComponentsDigital Components
Digital Components
lalithambiga kamaraj
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
lalithambiga kamaraj
 
Io management disk scheduling algorithm
Io management disk scheduling algorithmIo management disk scheduling algorithm
Io management disk scheduling algorithm
lalithambiga kamaraj
 
Recovery system
Recovery systemRecovery system
Recovery system
lalithambiga kamaraj
 
File management
File managementFile management
File management
lalithambiga kamaraj
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
lalithambiga kamaraj
 
Managing console of I/o operations & working with files
Managing console of I/o operations & working with filesManaging console of I/o operations & working with files
Managing console of I/o operations & working with files
lalithambiga kamaraj
 

More from lalithambiga kamaraj (20)

Firewall in Network Security
Firewall in Network SecurityFirewall in Network Security
Firewall in Network Security
 
Data Compression in Multimedia
Data Compression in MultimediaData Compression in Multimedia
Data Compression in Multimedia
 
Data CompressionMultimedia
Data CompressionMultimediaData CompressionMultimedia
Data CompressionMultimedia
 
Digital Audio in Multimedia
Digital Audio in MultimediaDigital Audio in Multimedia
Digital Audio in Multimedia
 
Network Security: Physical security
Network Security: Physical security Network Security: Physical security
Network Security: Physical security
 
Graphs in Data Structure
Graphs in Data StructureGraphs in Data Structure
Graphs in Data Structure
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Data structure
Data structureData structure
Data structure
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
 
Estimating Software Maintenance Costs
Estimating Software Maintenance CostsEstimating Software Maintenance Costs
Estimating Software Maintenance Costs
 
Datamining
DataminingDatamining
Datamining
 
Digital Components
Digital ComponentsDigital Components
Digital Components
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
Io management disk scheduling algorithm
Io management disk scheduling algorithmIo management disk scheduling algorithm
Io management disk scheduling algorithm
 
Recovery system
Recovery systemRecovery system
Recovery system
 
File management
File managementFile management
File management
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Managing console of I/o operations & working with files
Managing console of I/o operations & working with filesManaging console of I/o operations & working with files
Managing console of I/o operations & working with files
 

Recently uploaded

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 

Recently uploaded (20)

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 

Inheritance

  • 2. SYNOPSIS  INHERITANCE DEFINITION  SYNTAX INACCESS SPECIFIERS  PUBLIC INHERITANCE  PROTECTED INHERITANCE  PRIVATE INHERITANCE  TYPES OF INHERITANCE  SINGLE INHERITANCE  MULIPLE INHERITANCE  MULITLEVE INHERITANCE  HIERARCHICAL INHERITANCE  HYBRID INHERITANCE  DIAMOND INHERITANCE
  • 3. INHERITANCE DEFINITION • Derived quality and characteristics from parents or ancestors • Like you inherit features of your parents For example : “she had inherited the beauty of her Mother” • Inheritance in object oriented programming can be described as a process of creating new class from exiting classes • New classes inheritance some of the properties and behavior of the existing classes
  • 4. • 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 • Inheritance is a technique of code reuse • It also provides possibility to extend existing classes by creating derived classes
  • 5. SYNTAX • The basic syntax of inheritance is: class derived class : access specified base class • Access specified can be public , protected and private • The default access specified is private • Access specified affect accessibility of data member of bass class from the derived class
  • 6. INHERITANCE ACCESS SPECIFIERS PUBLIC INHERITANCE • This inheritance mode is used mostly • In this the protected member of base class becomes protected members of derived class and public become public • Drived class of derived classes: • If we are inheriting a derived class using a public inheriting as shows below class B:public A class C:public B
  • 7. • Then public and protected members of class A will be accessible in class C as public and protected respectively PROTECTED INHERITANCE • In protected mode the public and protected members of base class becomes protected members of derived class Example: class derived class: protected base class
  • 8. • If we are inheritance a derived class using a public inheritance as shown below class B:protected A class C:protected B • Then public and protected member of class • A will be accessible in class c as protected
  • 9. PRIVATE INHERITENCE • In private mode the public and protected member of base class become private members of derived class FOR EXAMPLE: class derived class : private base class class derived class : base class • If we are inheriting a derived class using a public inheritance as shows below FOR EXAMPLE: class B : private A class C : private B
  • 10. TYPES OF INHERITANCE • There are different type of inheritance  Single inheritance Multiple inheritance Multilevel inheritance Hierarchical inheritance Hybrid(virtual) inheritance
  • 11. SINGLE INHERITANCE • Single inheritance represents a from of inheritance when there is only one base class and one derived class FOR EXAMPLE: A class describes a person
  • 13. • We want to create new class student which should have the same information as person class plus one new information about uninersity • In this case we can create a derived should have the same information as person class plus one new information about university • we can create a derived class student
  • 14. • If you want to call the parameterized(user defined) • Constructor of a base class from a derived class then you need to write a parameterized constructor of a derived class as below • We can access member function of base class from a derived class FOR EXAMPLE: • We can create a new print() function in a derived class that uses printf() member function of a base class
  • 15. MULTIPLE INHERITANCE Multiple inheritance represents a kind of inheritance when a derived class inherits properties of multiple classes FOR EXAMPLE: • There are three classes A ,Band C and derived class is D as shows below Class A Class B Class c Class D
  • 16. MULTIPLE INHERITANCE PROGRAM • class derived class : access specified • base class 1.base class 2..base class n • You can create a new class that will • Inherit all the properties of all these class
  • 18. MULTILEVEL INHERITANCE  Multilevel inheritance represents a type of inheritance when a derived class is a base class for another class  In other words deriving a class from a derived class is know as multilevel inheritance  Simple multilevel inheritance is shown in below image  Where class A is a parent of class B and class B is a parent of class C
  • 20. • As you can see class person is the base class of both student employee classes • At the same time class student is the basic class for IT student and math student classes • Employee is the base class for derived and engineer classes
  • 21. HIERARCHICAL INHERTANCE • When there is a need to create multiple derived classes that inheritance properties of the same base class is known as hierarchical inheritance
  • 23. HYBRID INHERITANCE • Combination of multilevel and hierarchical inheritance will give you hybrid inheritance CLASS A CLASS B CLASS C CLASS D
  • 24. DIAMOND PROBLEM • When you have a hybrid inheritance then a diamond problem may arise • In this problem a derived class will have multiple paths to a base • This will result in duplicate inheritance member of the base class • This kind of problem is known as diamond problem
  • 26. VIRTUAL INHERITANCE Child classes in this case should inheritance grandparent class by using Virtual inheritance