SlideShare a Scribd company logo
Inheritance
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
2
Inheritance
 Inheritance can be defined as the process where one class
acquires the properties (methods and fields) of another.
 The class which inherits the properties of other is known
as subclass (derived class, child class) and the class whose
properties are inherited is known as superclass (base
class, parent class).
 Therefore, a subclass is a specialized version of a
superclass.
 It inherits all of the instance variables and methods
defined by the superclass and adds its own, unique
elements.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
3
Important terminologies
 Super Class: The class whose features are inherited.
 Sub Class: The class that inherits the other class.
 The subclass can add its own fields and methods in addition to the
superclass fields and methods.
 Reusability: Inheritance supports the concept of
“reusability”,
 i.e. when we want to create a new class and there is already a
class that includes some of the code that we want, we can derive
our new class from the existing class. By doing this, we are reusing
the fields and methods of the existing class.
 To inherit from a class, use the extends keyword.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
4
Syntax
 extends is the keyword used to inherit the properties of a
class.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
5
Syntax
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
6
Inheritance Demo
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
7
Inheritance Demo
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Try it!!
 Although a subclass includes all of the members of its
superclass, it cannot access those members of the
superclass that have been declared as private.
 The final Keyword
 If you don't want other classes to inherit from a class, use the
final keyword.
8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Using super to Call Superclass
 A subclass can call a constructor defined by its superclass
by use of the following form of super:
 arg-list specifies any arguments needed by the constructor
in the superclass.
 super() must always be the first statement executed
inside a subclass’ constructor.
9Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Using super to Call Superclass
10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Using super to access members
 A subclass can access the data members and methods of
superclass by use of the following form of super:
 member can be either a method or an instance variable.
 This second form of super is most applicable to situations
in which member names of a subclass hide (same name)
members by the same name in the superclass
11Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Using super to access members
12Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Types of Inheritance
13Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Single Inheritance
 When a class inherits another class, it is known as a single inheritance.
14Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Multi level Inheritance
 When there is a chain of inheritance, it is known as multilevel inheritance.
15Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Hierarchical Inheritance
 When two or more classes inherits a single class, it is hierarchical inheritance
16Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Multiple Inheritance
 To reduce the complexity and simplify the language, multiple
inheritance is not supported in java.
 Consider a scenario where A, B, and C are three classes.
 The C class inherits A and B classes.
 If A and B classes have the same method and you call it from child
class object, there will be ambiguity to call the method of A or B
class.
 In java, we can achieve multiple inheritance only through Interfaces.
17Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Method Overriding
 In a class hierarchy, when a method in a subclass has the same name
and type signature as a method in its superclass,
 then the method in the subclass is said to override the method in the
superclass.
 When an overridden method is called from within a subclass, it will
always refer to the version of that method defined by the subclass.
 The version of the method defined by the superclass will be hidden.
18Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Method Overriding
How to call the show()
method of Class A????
Two possible ways!!!!!
19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Final Keyword
 The keyword final has three uses.
 First, it can be used to create the equivalent of a named
constant.
 The other two uses of final apply to inheritance.
 Using final to Prevent Overriding
 Using final to Prevent Inheritance
20Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Final Keyword
21Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Important facts about inheritance
 Default superclass: Except Object class, which has no superclass,
every class has one and only one direct superclass (single
inheritance). In the absence of any other explicit superclass, every
class is implicitly a subclass of Object class.
 Superclass can only be one: A superclass can have any number of
subclasses. But a subclass can have only one superclass. This is
because Java does not support multiple inheritance with classes.
 Inheriting Constructors: A subclass inherits all the members (fields,
methods, and nested classes) from its superclass. Constructors are
not members, so they are not inherited by subclasses, but the
constructor of the superclass can be invoked from the subclass.
 Private member inheritance: A subclass does not inherit the private
members of its parent class. However, if the superclass has public or
protected methods for accessing its private fields, these can also be
used by the subclass.
22Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
The instanceof Keyword
 The java instanceof
operator is used to test
whether the object is an
instance of the specified
type (class or subclass or
interface).
 The instanceof in java is
also known as type
comparison operator
because it compares the
instance with type. It
returns either true or
false.
23Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
The Object Class
 There is one special class, Object, defined by Java.
 All other classes are subclasses of Object.
 That is, Object is a superclass of all other classes. This
means that a reference variable of type
 Object can refer to an object of any other class.
Check what are the methods associated with Object class!!!!
24Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
The End…
25Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam

More Related Content

PPTX
Class properties
Siva Priya
 
PPT
Unit 3 Java
arnold 7490
 
PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
PPTX
Inheritance in oops
Hirra Sultan
 
PDF
Lecture 10
Debasish Pratihari
 
PDF
Inheritance used in java
TharuniDiddekunta
 
PDF
java-06inheritance
Arjun Shanka
 
Class properties
Siva Priya
 
Unit 3 Java
arnold 7490
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
Inheritance in oops
Hirra Sultan
 
Lecture 10
Debasish Pratihari
 
Inheritance used in java
TharuniDiddekunta
 
java-06inheritance
Arjun Shanka
 

What's hot (17)

PDF
Top 10 java_oops_interview_questions
nishajj
 
PPTX
Top 10 java oops interview questions
nishajj
 
DOC
Java classes and objects interview questions
Dhivyashree Selvarajtnkpm
 
PDF
Common Programming Errors by Beginners in Java
Ravi_Kant_Sahu
 
PPT
OOPs Lecture 2
Abbas Ajmal
 
PPT
Inheritance and Polymorphism
BG Java EE Course
 
PDF
Multiple Inheritance
adil raja
 
PPTX
Inheritance
prashant prath
 
PDF
Learn C# Programming - Classes & Inheritance
Eng Teong Cheah
 
PDF
EEE oops Vth semester viva questions with answer
Jeba Moses
 
PDF
Packages
Ravi Kant Sahu
 
PDF
Python - object oriented
Learnbay Datascience
 
PPTX
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
PDF
Interfaces in java
TharuniDiddekunta
 
PPT
inheritance
Mohit Patodia
 
PPT
Inheritance, Object Oriented Programming
Arslan Waseem
 
PPTX
Final presentation programming
haider ali
 
Top 10 java_oops_interview_questions
nishajj
 
Top 10 java oops interview questions
nishajj
 
Java classes and objects interview questions
Dhivyashree Selvarajtnkpm
 
Common Programming Errors by Beginners in Java
Ravi_Kant_Sahu
 
OOPs Lecture 2
Abbas Ajmal
 
Inheritance and Polymorphism
BG Java EE Course
 
Multiple Inheritance
adil raja
 
Inheritance
prashant prath
 
Learn C# Programming - Classes & Inheritance
Eng Teong Cheah
 
EEE oops Vth semester viva questions with answer
Jeba Moses
 
Packages
Ravi Kant Sahu
 
Python - object oriented
Learnbay Datascience
 
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
Interfaces in java
TharuniDiddekunta
 
inheritance
Mohit Patodia
 
Inheritance, Object Oriented Programming
Arslan Waseem
 
Final presentation programming
haider ali
 
Ad

Similar to Java - Inheritance Concepts (20)

PPTX
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
PDF
JAVA UNIT 2 BCA students' notes IPU university
n32310997
 
PPTX
Ch5 inheritance
HarshithaAllu
 
PPTX
object oriented programming unit two ppt
isiagnel2
 
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
PPT
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
PDF
Unit 2
Amar Jukuntla
 
PPTX
Lec 1.6 Object Oriented Programming
Badar Waseer
 
PPTX
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
PDF
Inheritance
Sardar Alam
 
PPTX
Inheritance Slides
Ahsan Raja
 
PDF
Inheritance and interface
Shubham Sharma
 
PPT
04inherit
Waheed Warraich
 
PPTX
Chapter 3i
siragezeynu
 
PPT
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
AshwathGupta
 
PDF
Java programming -Object-Oriented Thinking- Inheritance
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPTX
UNIT 5.pptx
CurativeServiceDivis
 
PPTX
Chapter 9 java
Ahmad sohail Kakar
 
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
JAVA UNIT 2 BCA students' notes IPU university
n32310997
 
Ch5 inheritance
HarshithaAllu
 
object oriented programming unit two ppt
isiagnel2
 
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
Lec 1.6 Object Oriented Programming
Badar Waseer
 
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
Inheritance
Sardar Alam
 
Inheritance Slides
Ahsan Raja
 
Inheritance and interface
Shubham Sharma
 
04inherit
Waheed Warraich
 
Chapter 3i
siragezeynu
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
AshwathGupta
 
Java programming -Object-Oriented Thinking- Inheritance
Jyothishmathi Institute of Technology and Science Karimnagar
 
Chapter 9 java
Ahmad sohail Kakar
 
Ad

More from Victer Paul (13)

PDF
OOAD - UML - Sequence and Communication Diagrams - Lab
Victer Paul
 
PDF
OOAD - UML - Class and Object Diagrams - Lab
Victer Paul
 
PDF
OOAD - Systems and Object Orientation Concepts
Victer Paul
 
PDF
Java - Strings Concepts
Victer Paul
 
PDF
Java - Packages Concepts
Victer Paul
 
PDF
Java - OOPS and Java Basics
Victer Paul
 
PDF
Java - Exception Handling Concepts
Victer Paul
 
PDF
Java - Class Structure
Victer Paul
 
PDF
Java - Object Oriented Programming Concepts
Victer Paul
 
PDF
Java - Basic Concepts
Victer Paul
 
PDF
Java - File Input Output Concepts
Victer Paul
 
PDF
Java - Arrays Concepts
Victer Paul
 
PDF
Java applet programming concepts
Victer Paul
 
OOAD - UML - Sequence and Communication Diagrams - Lab
Victer Paul
 
OOAD - UML - Class and Object Diagrams - Lab
Victer Paul
 
OOAD - Systems and Object Orientation Concepts
Victer Paul
 
Java - Strings Concepts
Victer Paul
 
Java - Packages Concepts
Victer Paul
 
Java - OOPS and Java Basics
Victer Paul
 
Java - Exception Handling Concepts
Victer Paul
 
Java - Class Structure
Victer Paul
 
Java - Object Oriented Programming Concepts
Victer Paul
 
Java - Basic Concepts
Victer Paul
 
Java - File Input Output Concepts
Victer Paul
 
Java - Arrays Concepts
Victer Paul
 
Java applet programming concepts
Victer Paul
 

Recently uploaded (20)

PDF
Architecture of the Future (09152021)
EdwardMeyman
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PPTX
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
 
PDF
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Architecture of the Future (09152021)
EdwardMeyman
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
 
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
This slide provides an overview Technology
mineshkharadi333
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 

Java - Inheritance Concepts

  • 1. Inheritance Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 2. 2 Inheritance  Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another.  The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).  Therefore, a subclass is a specialized version of a superclass.  It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements. Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 3. 3 Important terminologies  Super Class: The class whose features are inherited.  Sub Class: The class that inherits the other class.  The subclass can add its own fields and methods in addition to the superclass fields and methods.  Reusability: Inheritance supports the concept of “reusability”,  i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.  To inherit from a class, use the extends keyword. Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 4. 4 Syntax  extends is the keyword used to inherit the properties of a class. Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 5. 5 Syntax Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 6. 6 Inheritance Demo Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 7. 7 Inheritance Demo Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 8. Try it!!  Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private.  The final Keyword  If you don't want other classes to inherit from a class, use the final keyword. 8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 9. Using super to Call Superclass  A subclass can call a constructor defined by its superclass by use of the following form of super:  arg-list specifies any arguments needed by the constructor in the superclass.  super() must always be the first statement executed inside a subclass’ constructor. 9Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 10. Using super to Call Superclass 10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 11. Using super to access members  A subclass can access the data members and methods of superclass by use of the following form of super:  member can be either a method or an instance variable.  This second form of super is most applicable to situations in which member names of a subclass hide (same name) members by the same name in the superclass 11Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 12. Using super to access members 12Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 13. Types of Inheritance 13Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 14. Single Inheritance  When a class inherits another class, it is known as a single inheritance. 14Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 15. Multi level Inheritance  When there is a chain of inheritance, it is known as multilevel inheritance. 15Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 16. Hierarchical Inheritance  When two or more classes inherits a single class, it is hierarchical inheritance 16Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 17. Multiple Inheritance  To reduce the complexity and simplify the language, multiple inheritance is not supported in java.  Consider a scenario where A, B, and C are three classes.  The C class inherits A and B classes.  If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.  In java, we can achieve multiple inheritance only through Interfaces. 17Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 18. Method Overriding  In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass,  then the method in the subclass is said to override the method in the superclass.  When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.  The version of the method defined by the superclass will be hidden. 18Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 19. Method Overriding How to call the show() method of Class A???? Two possible ways!!!!! 19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 20. Final Keyword  The keyword final has three uses.  First, it can be used to create the equivalent of a named constant.  The other two uses of final apply to inheritance.  Using final to Prevent Overriding  Using final to Prevent Inheritance 20Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 21. Final Keyword 21Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 22. Important facts about inheritance  Default superclass: Except Object class, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object class.  Superclass can only be one: A superclass can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance with classes.  Inheriting Constructors: A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.  Private member inheritance: A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. 22Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 23. The instanceof Keyword  The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface).  The instanceof in java is also known as type comparison operator because it compares the instance with type. It returns either true or false. 23Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 24. The Object Class  There is one special class, Object, defined by Java.  All other classes are subclasses of Object.  That is, Object is a superclass of all other classes. This means that a reference variable of type  Object can refer to an object of any other class. Check what are the methods associated with Object class!!!! 24Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 25. The End… 25Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam