SlideShare a Scribd company logo
Inheritance: Definition
Inheritance: A parent-child
relationship between classes allows
sharing of the behavior of the parent
class into its child classes
one of the major benefits of object-
oriented programming (OOP) is this code
sharing between classes through
inheritance
Child class can add new behavior or
override existing behavior from parent
Reusability- building new components
by utilising existing components- is yet
another important aspect of OO
paradigm.
22-03-2018SALEEMQAISAR
1
Parent
Child
Inheritance terms
subclass, derived class, child class: Terms to describe the child in
the relationship, which accepts functionality from its parent.
class SubClassName extends SuperClassName
{
fields declaration;
methods declaration;
}
The keyword “extends” signifies that the properties of super class
are extended to the subclass. That means, subclass contains its own
members as well of those of the super class. This kind of situation
occurs when we want to enhance properties of existing class
without actually modifying it.
22-03-2018SALEEMQAISAR
2
Syntax Of Inheritance
public class Class extends ParentClass
{
//new variable or methods here
}
Example:
22-03-2018SALEEMQAISAR
3
superclass, base class, parent class: terms to describe the
parent in the relationship, which shares its functionality
extend, inherit, derive: Become a subclass of another class.
22-03-2018SALEEMQAISAR
4
Types Of Inheritance
Single inheritance
Multiple inheritance
Hierarchical inheritance
Multi-Level inheritance
Hybrid inheritance
Multi-path inheritance
22-03-2018SALEEMQAISAR
5
Single inheritance
Single Inheritance is the simple
inheritance of all, When a class
extends another class(Only one
class) then we call it as Single
inheritance. The below diagram
represents the single inheritance
in java where Class B extends
only one class Class
A. Here Class B will be the Sub
class and Class A will be one and
only Super class.
22-03-2018SALEEMQAISAR
6
Multiple inheritance
Multiple Inheritance is nothing
but one class extending more tha
n one class. Multiple Inheritance
is basically not supported by
many Object Oriented
Programming languages such
as Java, Small Talk, C# etc.. (C++
Supports Multiple Inheritance). As
the Child class has to manage the
dependency of more than
one Parent class. But you can
achieve multiple inheritance in
Java using Interfaces.
22-03-2018SALEEMQAISAR
7
Multi-Level inheritance
In Multilevel Inheritance a
derived class will be inheriting a
parent class and as well as the
derived class act as the parent
class to other class. As seen in
the below diagram. Class B
inherits the property of Class
A and again Class B act as a
parent for Class C. In Short Class
A parent for Class B and Class
B parent for Class C.
22-03-2018SALEEMQAISAR
8
Hierarchical inheritance
In Hierarchical
inheritance one parent
class will be inherited
by many sub classes.
As per the below
example Class A will be
inherited by Class B,
Class C and Class
D. Class A will be acting
as a parent class
for Class B, Class
C and Class D.
22-03-2018SALEEMQAISAR
9
Hybrid Inheritance
Hybrid Inheritance is the
combination of both Single
and Multiple Inheritance.
Again Hybrid inheritance is
also not directly supported
in Java only through
interface we can achieve
this. Flow diagram of the
Hybrid inheritance will look
like below. As you can Class
A will be acting as
the Parent class for Class B &
Class C and Class B & Class
C will be acting
as Parent for Class D.
22-03-2018SALEEMQAISAR
10
Access modifiers
Java provides a number of access modifiers to set access
levels for classes, variables, methods, and constructors. The
four access levels are:
Default Access Modifier - No Keyword
Private Access Modifier - Private
Public Access Modifier - Public
Protected Access Modifier - Protected
22-03-2018SALEEMQAISAR
11
Default - No Keyword
Default access modifier means we do not explicitly declare an
access modifier for a class, field, method, etc.
A variable or method declared without any access control
modifier is available to any other class in the same package.
The fields in an interface are implicitly public static final and
the methods in an interface are by default public.
Example:
Variables and methods can be declared without any modifiers, as
in the following examples −
String version = "1.5.1";
boolean processOrder()
{ return true;
}
22-03-2018SALEEMQAISAR
12
Access Modifier - Private
Methods, variables, and constructors that are declared private
can only be accessed within the declared class itself.
Private access modifier is the most restrictive access level.
Class and interfaces cannot be private.
Private access modifier is the most restrictive access level.
Class and interfaces cannot be private.
Using the private modifier is the main way that an object
encapsulates itself and hides data from the outside world.
22-03-2018SALEEMQAISAR
13
Example
public class Logger
{
private String format;
public String getFormat()
{
return this.format;
}
public void setFormat(String format)
{
this.format = format;
}
}
22-03-2018SALEEMQAISAR
14
Access Modifier - Public
A class, method, constructor, interface, etc. declared public
can be accessed from any other class. Therefore, fields,
methods, blocks declared inside a public class can be accessed
from any class belonging to the Java Universe.
However, if the public class we are trying to access is in a
different package, then the public class still needs to be
imported. Because of class inheritance, all public methods and
variables of a class are inherited by its subclasses.
Example:
public static void main(String[] arguments) {
// ...
}
22-03-2018SALEEMQAISAR
15
Access Modifier - Protected
Variables, methods, and constructors, which are declared
protected in a superclass can be accessed only by the
subclasses in other package or any class within the package of
the protected members' class.
The protected access modifier cannot be applied to class and
interfaces. Methods, fields can be declared protected,
however methods and fields in a interface cannot be declared
protected.
Protected access gives the subclass a chance to use the helper
method or variable, while preventing a nonrelated class from
trying to use it.
22-03-2018SALEEMQAISAR
16
Example
The following parent class uses protected access control, to
allow its child class override openSpeaker() method −
class AudioPlayer
{
protected boolean openSpeaker(Speaker sp)
{
// implementation details
}
}
class StreamingAudioPlayer { boolean openSpeaker(Speaker sp)
{ // implementation details } }
22-03-2018SALEEMQAISAR
17
THANKS
ANY Q??
22-03-2018SALEEMQAISAR
18

More Related Content

What's hot

Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
Vishesh Jha
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Inheritance
InheritanceInheritance
Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Languagedheva B
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
Githushan Gengaparam
 
Exception handling
Exception handlingException handling
Exception handling
Pranali Chaudhari
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
Spotle.ai
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
Burhan Ahmed
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Harsh Patel
 
Aggregation v s composition
Aggregation v s compositionAggregation v s composition
Aggregation v s composition
SayedFarhan110
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
Md. Tanvir Hossain
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 

What's hot (20)

Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance
InheritanceInheritance
Inheritance
 
Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Language
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Exception handling
Exception handlingException handling
Exception handling
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Inheritance
InheritanceInheritance
Inheritance
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
OOP java
OOP javaOOP java
OOP java
 
Aggregation v s composition
Aggregation v s compositionAggregation v s composition
Aggregation v s composition
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 

Similar to Inheritance and its types In Java

Access Modifiers .pptx
Access Modifiers .pptxAccess Modifiers .pptx
Access Modifiers .pptx
MDRakibKhan3
 
Final presentation programming
Final presentation programmingFinal presentation programming
Final presentation programming
haider ali
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
RumiHossain5
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
Sourabrata Mukherjee
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
Hirra Sultan
 
Chapter 3i
Chapter 3iChapter 3i
Chapter 3i
siragezeynu
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
Arati Gadgil
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
Ganesh Amirineni
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2sotlsoc
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
WaqarRaj1
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
talha ijaz
 
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eEo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5e
Gina Bullock
 
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eEo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5e
Gina Bullock
 
C++ Inheritance.pptx
C++ Inheritance.pptxC++ Inheritance.pptx
C++ Inheritance.pptx
XanGwaps
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
LadallaRajKumar
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
Thakur Amit Tomer
 
Inheritance and its necessity in java.ppt
Inheritance and its necessity in java.pptInheritance and its necessity in java.ppt
Inheritance and its necessity in java.ppt
ssuserf170c4
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
HarshitaAshwani
 

Similar to Inheritance and its types In Java (20)

Access Modifiers .pptx
Access Modifiers .pptxAccess Modifiers .pptx
Access Modifiers .pptx
 
Final presentation programming
Final presentation programmingFinal presentation programming
Final presentation programming
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Chapter 3i
Chapter 3iChapter 3i
Chapter 3i
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eEo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5e
 
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eEo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5e
 
C++ Inheritance.pptx
C++ Inheritance.pptxC++ Inheritance.pptx
C++ Inheritance.pptx
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Inheritance and its necessity in java.ppt
Inheritance and its necessity in java.pptInheritance and its necessity in java.ppt
Inheritance and its necessity in java.ppt
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 

Recently uploaded

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
"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
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
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
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
ShivajiThube2
 
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
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 

Recently uploaded (20)

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
"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...
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
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 ...
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
 
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
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 

Inheritance and its types In Java

  • 1. Inheritance: Definition Inheritance: A parent-child relationship between classes allows sharing of the behavior of the parent class into its child classes one of the major benefits of object- oriented programming (OOP) is this code sharing between classes through inheritance Child class can add new behavior or override existing behavior from parent Reusability- building new components by utilising existing components- is yet another important aspect of OO paradigm. 22-03-2018SALEEMQAISAR 1 Parent Child
  • 2. Inheritance terms subclass, derived class, child class: Terms to describe the child in the relationship, which accepts functionality from its parent. class SubClassName extends SuperClassName { fields declaration; methods declaration; } The keyword “extends” signifies that the properties of super class are extended to the subclass. That means, subclass contains its own members as well of those of the super class. This kind of situation occurs when we want to enhance properties of existing class without actually modifying it. 22-03-2018SALEEMQAISAR 2
  • 3. Syntax Of Inheritance public class Class extends ParentClass { //new variable or methods here } Example: 22-03-2018SALEEMQAISAR 3
  • 4. superclass, base class, parent class: terms to describe the parent in the relationship, which shares its functionality extend, inherit, derive: Become a subclass of another class. 22-03-2018SALEEMQAISAR 4
  • 5. Types Of Inheritance Single inheritance Multiple inheritance Hierarchical inheritance Multi-Level inheritance Hybrid inheritance Multi-path inheritance 22-03-2018SALEEMQAISAR 5
  • 6. Single inheritance Single Inheritance is the simple inheritance of all, When a class extends another class(Only one class) then we call it as Single inheritance. The below diagram represents the single inheritance in java where Class B extends only one class Class A. Here Class B will be the Sub class and Class A will be one and only Super class. 22-03-2018SALEEMQAISAR 6
  • 7. Multiple inheritance Multiple Inheritance is nothing but one class extending more tha n one class. Multiple Inheritance is basically not supported by many Object Oriented Programming languages such as Java, Small Talk, C# etc.. (C++ Supports Multiple Inheritance). As the Child class has to manage the dependency of more than one Parent class. But you can achieve multiple inheritance in Java using Interfaces. 22-03-2018SALEEMQAISAR 7
  • 8. Multi-Level inheritance In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the derived class act as the parent class to other class. As seen in the below diagram. Class B inherits the property of Class A and again Class B act as a parent for Class C. In Short Class A parent for Class B and Class B parent for Class C. 22-03-2018SALEEMQAISAR 8
  • 9. Hierarchical inheritance In Hierarchical inheritance one parent class will be inherited by many sub classes. As per the below example Class A will be inherited by Class B, Class C and Class D. Class A will be acting as a parent class for Class B, Class C and Class D. 22-03-2018SALEEMQAISAR 9
  • 10. Hybrid Inheritance Hybrid Inheritance is the combination of both Single and Multiple Inheritance. Again Hybrid inheritance is also not directly supported in Java only through interface we can achieve this. Flow diagram of the Hybrid inheritance will look like below. As you can Class A will be acting as the Parent class for Class B & Class C and Class B & Class C will be acting as Parent for Class D. 22-03-2018SALEEMQAISAR 10
  • 11. Access modifiers Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. The four access levels are: Default Access Modifier - No Keyword Private Access Modifier - Private Public Access Modifier - Public Protected Access Modifier - Protected 22-03-2018SALEEMQAISAR 11
  • 12. Default - No Keyword Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public. Example: Variables and methods can be declared without any modifiers, as in the following examples − String version = "1.5.1"; boolean processOrder() { return true; } 22-03-2018SALEEMQAISAR 12
  • 13. Access Modifier - Private Methods, variables, and constructors that are declared private can only be accessed within the declared class itself. Private access modifier is the most restrictive access level. Class and interfaces cannot be private. Private access modifier is the most restrictive access level. Class and interfaces cannot be private. Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world. 22-03-2018SALEEMQAISAR 13
  • 14. Example public class Logger { private String format; public String getFormat() { return this.format; } public void setFormat(String format) { this.format = format; } } 22-03-2018SALEEMQAISAR 14
  • 15. Access Modifier - Public A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe. However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses. Example: public static void main(String[] arguments) { // ... } 22-03-2018SALEEMQAISAR 15
  • 16. Access Modifier - Protected Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected. Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it. 22-03-2018SALEEMQAISAR 16
  • 17. Example The following parent class uses protected access control, to allow its child class override openSpeaker() method − class AudioPlayer { protected boolean openSpeaker(Speaker sp) { // implementation details } } class StreamingAudioPlayer { boolean openSpeaker(Speaker sp) { // implementation details } } 22-03-2018SALEEMQAISAR 17