SlideShare a Scribd company logo
Prepared by: Sharifullah “Durrani”
Comsats institute of information Technology
Abbottabad Pakistan
© Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.
Inheritance in java
 Inheritance
 A form of software reuse in which a new class is created by absorbing an existing
class’s members and embellishing them with new or modified capabilities.
 Can save time during program development by basing new classes on existing proven
and debugged high-quality software.
 Increases the likelihood that a system will be implemented and maintained effectively.
4
 Inheritance allows a software developer to
derive a new class from an existing one
 The existing class is called the parent class,
or superclass, or base class
 The derived class is called the child class or
subclass.
 As the name implies, the child inherits
characteristics of the parent
 That is, the child class inherits the methods
and data defined for the parent class
 When creating a class, rather than declaring completely new members, you can
designate that the new class should inherit the members of an existing class.
 Existing class is the superclass
 New class is the subclass
 Each subclass can be a superclass of future subclasses.
 A subclass can add its own fields and methods.
 A subclass is more specific than its superclass and represents a more specialized
group of objects.
 inheritance is sometimes referred to as specialization.
6
 Inheritance relationships often are shown
graphically in a UML class diagram, with an
arrow with an open arrowhead pointing to the
parent class
Inheritance should create an is-a relationship, meaning the child is a more
specific version of the parent
Vehicle
Car
7
 In Java, we use the reserved word extends to
establish an inheritance relationship
class Car extends Vehicle
{
// class contents
}
8
 Visibility modifiers determine which class members are
inherited and which are not
 Variables and methods declared with public visibility are
inherited; those with private visibility are not
 But public variables violate the principle of encapsulation
 There is a third visibility modifier that helps in inheritance
situations: protected
9
 The protected modifier allows a member of a
base class to be inherited into a child
 Protected visibility provides more
encapsulation than public visibility does
 However, protected visibility is not as tightly
encapsulated as private visibility
 Protected variables and methods can be shown
with a # symbol preceding them in UML
diagrams
10
 Constructors are not inherited, even though they have public
visibility
 Yet we often want to use the parent's constructor to set up
the "parent's part" of the object
 The super reference can be used to refer to the parent class,
and often is used to invoke the parent's constructor
 A child’s constructor is responsible for calling the parent’s
constructor
 The first line of a child’s constructor should use the super
reference to call the parent’s constructor
 The super reference can also be used to reference other
variables and methods defined in the parent’s class
 The direct superclass is the superclass from which the subclass explicitly
inherits.
 An indirect superclass is any class above the direct superclass in the class
hierarchy.
 The Java class hierarchy begins with class Object (in package
java.lang)
 Every class in Java directly or indirectly extends (or “inherits from”) Object.
 Java supports only single inheritance, in which each class is derived from
exactly one direct superclass.
 We distinguish between the is-a relationship and the has-a relationship
 Is-a represents inheritance
 In an is-a relationship, an object of a subclass can also be treated as an object of its
superclass
 Has-a represents composition
 In a has-a relationship, an object contains as members references to other objects
 Examples of superclasses and subclasses
 Superclasses tend to be “more general” and subclasses “more specific.”
 Because every subclass object is an object of its superclass, and one
superclass can have many subclasses,
 The set of objects represented by a superclass is typically larger than the set
of objects represented by any of its subclasses.
Inheritance in java
A superclass exists in a hierarchical relationship with its subclasses.
Each arrow in the hierarchy represents an is-a relationship.
an Employee is a CommunityMember”
“a Teacher is a Faculty member.”
Superclasses and Subclasses
Shape inheritance hierarchy
A Triangle is a TwoDimensionalShape and is a
Shape
 Not every class relationship is an inheritance relationship.
 Has-a relationship
 Create classes by composition of existing classes.
 Example: Given the classes Employee, BirthDate and TelephoneNumber, it’s
improper to say that an Employee is a BirthDate or that an Employee is a
TelephoneNumber.
 However, an Employee has a BirthDate, and an Employee has a
TelephoneNumber.
 Objects of all classes that extend a common superclass can be treated as
objects of that superclass.
 Commonality expressed in the members of the superclass.
 Inheritance issue
 A subclass can inherit methods that it does not need or should not have.
 Even when a superclass method is appropriate for a subclass, that subclass often needs
a customized version of the method.
 The subclass can override (redefine) the superclass method with an appropriate
implementation.
 A class’s public members are accessible wherever the program has a reference
to an object of that class or one of its subclasses.
 A class’s private members are accessible only within the class itself.
 protected access is an intermediate level of access between public and
private.
 A superclass’s protected members can be accessed by members of that superclass, by
members of its subclasses and by members of other classes in the same package
 protected members also have package access.
 All public and protected superclass members retain their original access modifier when
they become members of the subclass.
 A superclass’s private members are hidden in its subclasses
 They can be accessed only through the public or protected methods inherited from the
superclass
 Subclass methods can refer to public and protected members inherited
from the superclass simply by using the member names.
 When a subclass method overrides an inherited superclass method, the superclass
method can be accessed from the subclass by preceding the superclass method
name with keyword super and a dot (.) separator.
 Java supports single inheritance, meaning
that a derived class can have only one parent
class
 Multiple inheritance allows a class to be
derived from two or more classes, inheriting
the members of all parents
 Collisions, such as the same variable name in
two parents, have to be resolved
 Java does not support multiple inheritance
 In most cases, the use of interfaces gives us
aspects of multiple inheritance without the
overhead
Inheritance in java
24
 A child class can override the definition of an inherited
method in favor of its own
 The new method must have the same signature as the
parent's method, but can have a different body
 The type of the object executing the method determines
which version of the method is invoked
 A parent method can be invoked explicitly using the super
reference
 If a method is declared with the final modifier, it cannot be
overridden
 The concept of overriding can be applied to data and is called
shadowing variables
 Shadowing variables should be avoided because it tends to
cause unnecessarily confusing code
26
 Don't confuse the concepts of overloading and
overriding
 Overloading deals with multiple methods with the
same name in the same class, but with different
signatures
 Overriding deals with two methods, one in a parent
class and one in a child class, that have the same
signature
 Overloading lets you define a similar operation in
different ways for different data
 Overriding lets you define a similar operation in
different ways for different object types
Inheritance in java
Assign the base class reference to a child
class object.
Inheritance in java
 the object can call the overriding methods of child class and
all the non-overridden methods of base class
 but it cannot call the methods which are newly declared in
the child class
 Argument list: The argument list of overriding method must
be same as that of the method in parent class.
 The data types of the arguments and their sequence should
be maintained as it is in the overriding method.
 Access Modifier: The Access Modifier of the overriding
method (method of subclass) cannot be more restrictive than
the overridden method of parent class.
 This is not
allowed as child
class disp
method is more
restrictive(protect
ed) than base
class(public)
 Its valid
 private, static and final methods cannot be
overridden as they are local to the class.
 However static methods can be re-declared in
the sub class.
 Binding of overridden methods happen at
runtime which is known as dynamic binding.
 super keyword is used for calling the parent
class method/constructor.
 super keyword
is used for
calling the
parent class
method/constr
uctor.
36
 A class called Object is defined in the java.lang package of
the Java standard class library
 All classes are derived from the Object class
 If a class is not explicitly defined to be the child of an existing
class, it is assumed to be the child of the Object class
 Therefore, the Object class is the ultimate root of all class
hierarchies
 The Object class contains a few useful methods, which are inherited
by all classes
 For example, the toString method is defined in the Object class
 Every time we have defined toString, we have actually been
overriding an existing definition
 The toString method in the Object class is defined to return a
string that contains the name of the object’s class together along with
some other information
 All objects are guaranteed to have a toString method via
inheritance
 Thus the println method can call toString for any object
that is passed to it

More Related Content

What's hot (20)

PPTX
Abstract Class & Abstract Method in Core Java
MOHIT AGARWAL
 
PPTX
This keyword in java
Hitesh Kumar
 
PPT
Inheritance C#
Raghuveer Guthikonda
 
PPS
String and string buffer
kamal kotecha
 
PPSX
Exception Handling
Reddhi Basu
 
PPTX
Virtual base class
Tech_MX
 
PPTX
Abstract Class Presentation
tigerwarn
 
PPTX
Member Function in C++
NikitaKaur10
 
PPTX
Visibility control in java
Tech_MX
 
PPTX
Interfaces in java
Abishek Purushothaman
 
PDF
Inheritance In Java
Arnab Bhaumik
 
PPTX
Inheritance
Sapna Sharma
 
PPTX
Java package
CS_GDRCST
 
PPS
Wrapper class
kamal kotecha
 
PPTX
Java Methods
OXUS 20
 
PDF
Constructors and Destructors
Dr Sukhpal Singh Gill
 
PPTX
Strings in Java
Abhilash Nair
 
PPTX
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 
PPTX
Polymorphism
Nochiketa Chakraborty
 
PPTX
classes and objects in C++
HalaiHansaika
 
Abstract Class & Abstract Method in Core Java
MOHIT AGARWAL
 
This keyword in java
Hitesh Kumar
 
Inheritance C#
Raghuveer Guthikonda
 
String and string buffer
kamal kotecha
 
Exception Handling
Reddhi Basu
 
Virtual base class
Tech_MX
 
Abstract Class Presentation
tigerwarn
 
Member Function in C++
NikitaKaur10
 
Visibility control in java
Tech_MX
 
Interfaces in java
Abishek Purushothaman
 
Inheritance In Java
Arnab Bhaumik
 
Inheritance
Sapna Sharma
 
Java package
CS_GDRCST
 
Wrapper class
kamal kotecha
 
Java Methods
OXUS 20
 
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Strings in Java
Abhilash Nair
 
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 
Polymorphism
Nochiketa Chakraborty
 
classes and objects in C++
HalaiHansaika
 

Similar to Inheritance in java (20)

PPT
Inheritance and its necessity in java.ppt
ssuserf170c4
 
PDF
Unit 2
Amar Jukuntla
 
PPTX
Java
Haripritha
 
PPTX
Chap3 inheritance
raksharao
 
PPT
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
PPTX
Inheritance Slides
Ahsan Raja
 
PPTX
object oriented programming unit two ppt
isiagnel2
 
PPTX
Inheritance and Polymorphism Java
M. Raihan
 
PPTX
Chapter 8.2
sotlsoc
 
PPT
Inheritance & Polymorphism - 1
PRN USM
 
PPT
L7 inheritance
teach4uin
 
PPT
L7 inheritance
teach4uin
 
PPT
Ap Power Point Chpt7
dplunkett
 
PPTX
Ch5 inheritance
HarshithaAllu
 
PPT
RajLec10.ppt
Rassjb
 
PPTX
Unit3 part2-inheritance
DevaKumari Vijay
 
PDF
Java programming -Object-Oriented Thinking- Inheritance
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPTX
Java session2
Rajeev Kumar
 
PPTX
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
Inheritance and its necessity in java.ppt
ssuserf170c4
 
Chap3 inheritance
raksharao
 
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
Inheritance Slides
Ahsan Raja
 
object oriented programming unit two ppt
isiagnel2
 
Inheritance and Polymorphism Java
M. Raihan
 
Chapter 8.2
sotlsoc
 
Inheritance & Polymorphism - 1
PRN USM
 
L7 inheritance
teach4uin
 
L7 inheritance
teach4uin
 
Ap Power Point Chpt7
dplunkett
 
Ch5 inheritance
HarshithaAllu
 
RajLec10.ppt
Rassjb
 
Unit3 part2-inheritance
DevaKumari Vijay
 
Java programming -Object-Oriented Thinking- Inheritance
Jyothishmathi Institute of Technology and Science Karimnagar
 
Java session2
Rajeev Kumar
 
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
Ad

Recently uploaded (20)

PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Ad

Inheritance in java

  • 1. Prepared by: Sharifullah “Durrani” Comsats institute of information Technology Abbottabad Pakistan © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.
  • 3.  Inheritance  A form of software reuse in which a new class is created by absorbing an existing class’s members and embellishing them with new or modified capabilities.  Can save time during program development by basing new classes on existing proven and debugged high-quality software.  Increases the likelihood that a system will be implemented and maintained effectively.
  • 4. 4  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or superclass, or base class  The derived class is called the child class or subclass.  As the name implies, the child inherits characteristics of the parent  That is, the child class inherits the methods and data defined for the parent class
  • 5.  When creating a class, rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class.  Existing class is the superclass  New class is the subclass  Each subclass can be a superclass of future subclasses.  A subclass can add its own fields and methods.  A subclass is more specific than its superclass and represents a more specialized group of objects.  inheritance is sometimes referred to as specialization.
  • 6. 6  Inheritance relationships often are shown graphically in a UML class diagram, with an arrow with an open arrowhead pointing to the parent class Inheritance should create an is-a relationship, meaning the child is a more specific version of the parent Vehicle Car
  • 7. 7  In Java, we use the reserved word extends to establish an inheritance relationship class Car extends Vehicle { // class contents }
  • 8. 8  Visibility modifiers determine which class members are inherited and which are not  Variables and methods declared with public visibility are inherited; those with private visibility are not  But public variables violate the principle of encapsulation  There is a third visibility modifier that helps in inheritance situations: protected
  • 9. 9  The protected modifier allows a member of a base class to be inherited into a child  Protected visibility provides more encapsulation than public visibility does  However, protected visibility is not as tightly encapsulated as private visibility  Protected variables and methods can be shown with a # symbol preceding them in UML diagrams
  • 10. 10  Constructors are not inherited, even though they have public visibility  Yet we often want to use the parent's constructor to set up the "parent's part" of the object  The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor
  • 11.  A child’s constructor is responsible for calling the parent’s constructor  The first line of a child’s constructor should use the super reference to call the parent’s constructor  The super reference can also be used to reference other variables and methods defined in the parent’s class
  • 12.  The direct superclass is the superclass from which the subclass explicitly inherits.  An indirect superclass is any class above the direct superclass in the class hierarchy.  The Java class hierarchy begins with class Object (in package java.lang)  Every class in Java directly or indirectly extends (or “inherits from”) Object.  Java supports only single inheritance, in which each class is derived from exactly one direct superclass.
  • 13.  We distinguish between the is-a relationship and the has-a relationship  Is-a represents inheritance  In an is-a relationship, an object of a subclass can also be treated as an object of its superclass  Has-a represents composition  In a has-a relationship, an object contains as members references to other objects
  • 14.  Examples of superclasses and subclasses  Superclasses tend to be “more general” and subclasses “more specific.”  Because every subclass object is an object of its superclass, and one superclass can have many subclasses,  The set of objects represented by a superclass is typically larger than the set of objects represented by any of its subclasses.
  • 16. A superclass exists in a hierarchical relationship with its subclasses. Each arrow in the hierarchy represents an is-a relationship. an Employee is a CommunityMember” “a Teacher is a Faculty member.” Superclasses and Subclasses
  • 17. Shape inheritance hierarchy A Triangle is a TwoDimensionalShape and is a Shape
  • 18.  Not every class relationship is an inheritance relationship.  Has-a relationship  Create classes by composition of existing classes.  Example: Given the classes Employee, BirthDate and TelephoneNumber, it’s improper to say that an Employee is a BirthDate or that an Employee is a TelephoneNumber.  However, an Employee has a BirthDate, and an Employee has a TelephoneNumber.
  • 19.  Objects of all classes that extend a common superclass can be treated as objects of that superclass.  Commonality expressed in the members of the superclass.  Inheritance issue  A subclass can inherit methods that it does not need or should not have.  Even when a superclass method is appropriate for a subclass, that subclass often needs a customized version of the method.  The subclass can override (redefine) the superclass method with an appropriate implementation.
  • 20.  A class’s public members are accessible wherever the program has a reference to an object of that class or one of its subclasses.  A class’s private members are accessible only within the class itself.  protected access is an intermediate level of access between public and private.  A superclass’s protected members can be accessed by members of that superclass, by members of its subclasses and by members of other classes in the same package  protected members also have package access.  All public and protected superclass members retain their original access modifier when they become members of the subclass.
  • 21.  A superclass’s private members are hidden in its subclasses  They can be accessed only through the public or protected methods inherited from the superclass  Subclass methods can refer to public and protected members inherited from the superclass simply by using the member names.  When a subclass method overrides an inherited superclass method, the superclass method can be accessed from the subclass by preceding the superclass method name with keyword super and a dot (.) separator.
  • 22.  Java supports single inheritance, meaning that a derived class can have only one parent class  Multiple inheritance allows a class to be derived from two or more classes, inheriting the members of all parents  Collisions, such as the same variable name in two parents, have to be resolved  Java does not support multiple inheritance  In most cases, the use of interfaces gives us aspects of multiple inheritance without the overhead
  • 24. 24  A child class can override the definition of an inherited method in favor of its own  The new method must have the same signature as the parent's method, but can have a different body  The type of the object executing the method determines which version of the method is invoked
  • 25.  A parent method can be invoked explicitly using the super reference  If a method is declared with the final modifier, it cannot be overridden  The concept of overriding can be applied to data and is called shadowing variables  Shadowing variables should be avoided because it tends to cause unnecessarily confusing code
  • 26. 26  Don't confuse the concepts of overloading and overriding  Overloading deals with multiple methods with the same name in the same class, but with different signatures  Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature  Overloading lets you define a similar operation in different ways for different data  Overriding lets you define a similar operation in different ways for different object types
  • 28. Assign the base class reference to a child class object.
  • 30.  the object can call the overriding methods of child class and all the non-overridden methods of base class  but it cannot call the methods which are newly declared in the child class
  • 31.  Argument list: The argument list of overriding method must be same as that of the method in parent class.  The data types of the arguments and their sequence should be maintained as it is in the overriding method.  Access Modifier: The Access Modifier of the overriding method (method of subclass) cannot be more restrictive than the overridden method of parent class.
  • 32.  This is not allowed as child class disp method is more restrictive(protect ed) than base class(public)
  • 34.  private, static and final methods cannot be overridden as they are local to the class.  However static methods can be re-declared in the sub class.  Binding of overridden methods happen at runtime which is known as dynamic binding.  super keyword is used for calling the parent class method/constructor.
  • 35.  super keyword is used for calling the parent class method/constr uctor.
  • 36. 36  A class called Object is defined in the java.lang package of the Java standard class library  All classes are derived from the Object class  If a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object class  Therefore, the Object class is the ultimate root of all class hierarchies
  • 37.  The Object class contains a few useful methods, which are inherited by all classes  For example, the toString method is defined in the Object class  Every time we have defined toString, we have actually been overriding an existing definition  The toString method in the Object class is defined to return a string that contains the name of the object’s class together along with some other information
  • 38.  All objects are guaranteed to have a toString method via inheritance  Thus the println method can call toString for any object that is passed to it