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

PPTX
Inheritance
PPTX
Polymorphism in Python
PPTX
Inheritance and Polymorphism
PPT
Inheritance, Object Oriented Programming
PPTX
Java- Nested Classes
PPT
Introduction to method overloading & method overriding in java hdm
PPTX
20.3 Java encapsulation
PPT
Java: Inheritance
Inheritance
Polymorphism in Python
Inheritance and Polymorphism
Inheritance, Object Oriented Programming
Java- Nested Classes
Introduction to method overloading & method overriding in java hdm
20.3 Java encapsulation
Java: Inheritance

What's hot (20)

PPT
Inheritance C#
PPTX
This keyword in java
PDF
Object-oriented Programming-with C#
PPTX
C# classes objects
PPT
Constraints In Sql
PPTX
Inheritance and Polymorphism Java
PPTX
Java Inheritance - sub class constructors - Method overriding
PPT
Generics in java
PPTX
Classes,object and methods java
PPTX
Access specifier
PPTX
Types of Constructor in C++
PPT
Class 7 - PHP Object Oriented Programming
PPTX
Chapter 07 inheritance
PPTX
inheritance
PPTX
Polymorphism presentation in java
PPTX
Interfaces in java
PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
PPTX
C# Inheritance
PPT
Java static keyword
PPTX
Ppt on this and super keyword
Inheritance C#
This keyword in java
Object-oriented Programming-with C#
C# classes objects
Constraints In Sql
Inheritance and Polymorphism Java
Java Inheritance - sub class constructors - Method overriding
Generics in java
Classes,object and methods java
Access specifier
Types of Constructor in C++
Class 7 - PHP Object Oriented Programming
Chapter 07 inheritance
inheritance
Polymorphism presentation in java
Interfaces in java
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
C# Inheritance
Java static keyword
Ppt on this and super keyword
Ad

Viewers also liked (6)

PDF
Java Inheritance
PPTX
Multiple inheritance possible in Java
PPT
Java inheritance
PDF
Introduction to Android Studio
PDF
Android studio
PPTX
Inheritance in JAVA PPT
Java Inheritance
Multiple inheritance possible in Java
Java inheritance
Introduction to Android Studio
Android studio
Inheritance in JAVA PPT
Ad

Similar to Inheritance in java (20)

PPT
Inheritance and its necessity in java.ppt
PDF
PPTX
PPTX
Chap3 inheritance
PPT
Chapter 5 (OOP Principles).ppt
PPTX
Inheritance Slides
PPTX
object oriented programming unit two ppt
PPTX
Chapter 8.2
PPT
Inheritance & Polymorphism - 1
PPT
L7 inheritance
PPT
L7 inheritance
PPT
Ap Power Point Chpt7
PPTX
Ch5 inheritance
PPT
RajLec10.ppt
PPTX
Unit3 part2-inheritance
PDF
Java programming -Object-Oriented Thinking- Inheritance
PPTX
Java session2
PPTX
encapsulation, inheritance, overriding, overloading
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
PPTX
Modules 333333333³3444444444444444444.pptx
Inheritance and its necessity in java.ppt
Chap3 inheritance
Chapter 5 (OOP Principles).ppt
Inheritance Slides
object oriented programming unit two ppt
Chapter 8.2
Inheritance & Polymorphism - 1
L7 inheritance
L7 inheritance
Ap Power Point Chpt7
Ch5 inheritance
RajLec10.ppt
Unit3 part2-inheritance
Java programming -Object-Oriented Thinking- Inheritance
Java session2
encapsulation, inheritance, overriding, overloading
‏‏‏‏‏‏oop lecture objectives will come.pdf
Modules 333333333³3444444444444444444.pptx

Recently uploaded (20)

PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Transform Your Business with a Software ERP System
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
AI in Product Development-omnex systems
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
ai tools demonstartion for schools and inter college
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Introduction to Artificial Intelligence
PTS Company Brochure 2025 (1).pdf.......
Design an Analysis of Algorithms I-SECS-1021-03
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Transform Your Business with a Software ERP System
2025 Textile ERP Trends: SAP, Odoo & Oracle
AI in Product Development-omnex systems
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Odoo POS Development Services by CandidRoot Solutions
How to Choose the Right IT Partner for Your Business in Malaysia
ai tools demonstartion for schools and inter college
How Creative Agencies Leverage Project Management Software.pdf
Odoo Companies in India – Driving Business Transformation.pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
wealthsignaloriginal-com-DS-text-... (1).pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
Introduction to Artificial Intelligence

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