SlideShare a Scribd company logo
2
Most read
6
Most read
8
Most read
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 (20)

PDF
Php array
Nikul Shah
 
PPT
Abstract class in java
Lovely Professional University
 
PPT
Java packages
Raja Sekhar
 
PPT
Concepts In Object Oriented Programming Languages
ppd1961
 
PPT
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
PDF
Method Overloading In Java
CharthaGaglani
 
PPTX
Java packages
BHUVIJAYAVELU
 
PPTX
Inheritance
Sapna Sharma
 
PPTX
Threads in JAVA
Haldia Institute of Technology
 
PDF
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
PPT
Object-oriented concepts
BG Java EE Course
 
PDF
Files in java
Muthukumaran Subramanian
 
PPTX
Inheritance In Java
Manish Sahu
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
Regular expressions in Python
Sujith Kumar
 
PPTX
WHAT IS ABSTRACTION IN JAVA
sivasundari6
 
PPTX
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
PDF
Wrapper classes
Ravi_Kant_Sahu
 
PDF
itft-Inheritance in java
Atul Sehdev
 
PPTX
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Php array
Nikul Shah
 
Abstract class in java
Lovely Professional University
 
Java packages
Raja Sekhar
 
Concepts In Object Oriented Programming Languages
ppd1961
 
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Method Overloading In Java
CharthaGaglani
 
Java packages
BHUVIJAYAVELU
 
Inheritance
Sapna Sharma
 
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Object-oriented concepts
BG Java EE Course
 
Inheritance In Java
Manish Sahu
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Regular expressions in Python
Sujith Kumar
 
WHAT IS ABSTRACTION IN JAVA
sivasundari6
 
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
Wrapper classes
Ravi_Kant_Sahu
 
itft-Inheritance in java
Atul Sehdev
 
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 

Similar to Inheritance and its types In Java (20)

PPT
5. Inheritances, Packages and Intefaces
Nilesh Dalvi
 
PDF
4th_class.pdf
RumiHossain5
 
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
PPTX
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
PPTX
Java(inheritance)
Pooja Bhojwani
 
PPTX
java part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPTX
Polymorphism
Nuha Noor
 
PPTX
chapter 5 concepts of object oriented programming
WondimuBantihun1
 
PPT
Java_notes.ppt
tuyambazejeanclaude
 
PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
PPTX
Java OOPS Concept
Richa Gupta
 
PPTX
object oriented programming unit two ppt
isiagnel2
 
PPTX
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
PPTX
Core java oop
Parth Shah
 
PPT
Core java day5
Soham Sengupta
 
PDF
Oops (inheritance&interface)
Muthukumaran Subramanian
 
PDF
Java programming -Object-Oriented Thinking- Inheritance
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPTX
Oop inheritance chapter 3
Narayana Swamy
 
PPTX
Inheritance
Munsif Ullah
 
5. Inheritances, Packages and Intefaces
Nilesh Dalvi
 
4th_class.pdf
RumiHossain5
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Java(inheritance)
Pooja Bhojwani
 
java part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Polymorphism
Nuha Noor
 
chapter 5 concepts of object oriented programming
WondimuBantihun1
 
Java_notes.ppt
tuyambazejeanclaude
 
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
Java OOPS Concept
Richa Gupta
 
object oriented programming unit two ppt
isiagnel2
 
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
Core java oop
Parth Shah
 
Core java day5
Soham Sengupta
 
Oops (inheritance&interface)
Muthukumaran Subramanian
 
Java programming -Object-Oriented Thinking- Inheritance
Jyothishmathi Institute of Technology and Science Karimnagar
 
Oop inheritance chapter 3
Narayana Swamy
 
Inheritance
Munsif Ullah
 
Ad

Recently uploaded (20)

PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Horarios de distribución de agua en julio
pegazohn1978
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Ad

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