SlideShare a Scribd company logo
Inheritance
1RAJESHREE KHANDE
2
Inheritance
 Inheritance allows a software developer to derive a new class
from an existing one.
 The existing class is called the parent class or superclass
 The derived class is called the child class or subclass.
 Creates an is-a relationship
The subclass is a more
specific version of the
Original.
Book
NovelDictionary
Mystery RomanceRAJESHREE KHANDE
Inheritance
 The child class inherits the methods and data defined
for the parent class
 The programmer can add new variables or methods,
or can modify the inherited ones.
 Software reuse is at the heart of inheritance.
 By using existing software components to create new
ones, we capitalize on all the effort that went into the
design, implementation, and testing of the existing
software
3RAJESHREE KHANDE
4
Deriving Subclasses
 In Java, we use the reserved word extends to
establish an inheritance relationship
class Dictionary extends Book
{
variable declaration;
method declaration;
}
Book
NovelDictionary
Mystery Romance
RAJESHREE KHANDE
Form of Inheritance
Single Inheritance Hierarchical Inheritance
A
B
A
DCB
5RAJESHREE KHANDE
Form of Inheritance
B
C
A
A
B
C
Multilvel Inheritance
Multiple Inheritance
(Interface)
6RAJESHREE KHANDE
public class Book
{
protected int pages = 1500;
public String message()
{
System.out.println(“Number of pages: ” + pages);
}
}
public class Dictionary extends Book
{
private int def = 52500;
public void defMessage()
{
System.out.println(“Number of definitions” + def );
System.out.println(“Def per page: ” + (def /pages));
}
}
Number of pages: 1500
Number of definitions: 52500
Definitions per page: 35
7RAJESHREE KHANDE
Some Inheritance Details
 An instance of a child class does not rely on
an instance of a parent class
 Hence we could create a Dictionary object
without having to create a Book object first
 Inheritance is a one-way street
 The Book class cannot use variables or
methods declared explicitly in the Dictionary
class
8RAJESHREE KHANDE
9
The protected Modifier
 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
RAJESHREE KHANDE
10
The protected Modifier
 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
 the best possible encapsulation that permits
inheritance
RAJESHREE KHANDE
11
The super Reference
 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.
RAJESHREE KHANDE
The super Reference
 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
12RAJESHREE KHANDE
public class Book
{
protected int pages;
Book(int numPages)
{
pages = numPages;
}
}
public class Dictionary
{
private int def;
Dictionary(int numPages, int numDef )
{
super(numPages);
def = numDef ;
}
}
13RAJESHREE KHANDE
Multiple Inheritance
 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
14RAJESHREE KHANDE
15
Overriding Methods
 When a child class defines a method with the same
name and signature as a method in the parent class,
we say that the child’s version overrides the parent’s
version in favor of its own.
 Signature: method’s name along with number,
type, and order of its parameters
 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
RAJESHREE KHANDE
Overriding Methods
 A parent method can be invoked explicitly using the
super reference
 If a method is declared with the final modifier, it
cannot be overridden.
16RAJESHREE KHANDE
public class Book
{
protected int pages;
Book(int numPages)
{
pages = numPages;
}
public void message()
{
System.out.println(“Number of pages: ” + pages);
}
}
public class Dictionary extends Book
{
protected int definitions;
Dictionary(int numPages, int numDefinitions)
{
super(numPages);
definitions = numDefinitions;
}
public void message()
{
System.out.println(“Number of definitions” + definitions);
System.out.println(“Definitions per page: ” + (definitions/pages));
super.message();
}
}
17RAJESHREE KHANDE
18
Overloading vs. Overriding
 Overloading deals with
multiple methods in the
same class with the
same name but
different signatures
 Overloading lets you
define a similar
operation in different
ways for different data
 Overriding deals with
two methods, one in a
parent class and one in
a child class, that have
the same signature
 Overriding lets you
define a similar
operation in different
ways for different object
types
RAJESHREE KHANDE
19
Overloading vs. 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
RAJESHREE KHANDE
20
Class Hierarchies
 A child class of one parent can be the parent
of another child, forming a class hierarchy
Book
NovelDictionary
Mystery Romance
RAJESHREE KHANDE
21
The Object Class
 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
RAJESHREE KHANDE
The Object Class
 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
22RAJESHREE KHANDE
The Object Class
 The equals method of the Object class returns
true if two references are aliases.
 We can override equals in any class to define
equality in some more appropriate way
 The String class (as we've seen) defines the
equals method to return true if two String objects
contain the same characters
 Therefore the String class has overridden the
equals method inherited from Object in favor of its
own version
23RAJESHREE KHANDE
Abstract Class
 Sometimes, a class that you define represents an abstract concept
and, should not be instantiated.
 Take, for example, food in the real world. Have you ever seen an
instance of food? No. What you see instead are instances of
carrot, apple, and chocolate.
 Food represents the abstract concept of things that we can eat. It
doesn't make sense for an instance of food to exist.
 Similarly in object-oriented programming, you may want to model
abstract concepts but you don't want to be able to create an
instance of it.
24RAJESHREE KHANDE
Abstract Class
 For example, the Number class in the java.lang package represents
the abstract concept of numbers.
 It makes sense to model numbers in a program, but it doesn't make
sense to create a generic number object.
 Instead, the Number class makes sense only as a superclass to
classes like Integer and Float which implement specific kinds of
numbers.
 Classes such as Number, which implement abstract concepts and
should not be instantiated, are called abstract classes.
25RAJESHREE KHANDE
Abstract Class
 An abstract class is a class that can only be subclassed--it
cannot be instantiated.
 To declare that your class is an abstract class, use the keyword
abstract before the class keyword in your class declaration:
 Syntax
abstract class Number
{ . . . }
If you attempt to instantiate an abstract class, the compiler will
display an error similar to the following and refuse to compile
your program:
26RAJESHREE KHANDE
Abstract Method
 An abstract class may contain abstract methods. i.e methods
with no implementation
 An abstract class can define a complete programming interface,
thereby providing its subclasses with the method declarations
for all of the methods necessary to implement that programming
interface.
 The abstract class can leave some or all of the implementation
details of those methods up to its subclasses
27RAJESHREE KHANDE
Abstract Class :Example
28RAJESHREE KHANDE
Each non-abstract
subclass of
GraphicObject, such
as Circle and
Rectangle, would
have to provide an
implementation for
the draw method
abstract class GraphicObject
{ int x, y; . . .
abstract void draw();
}
class Circle extends GraphicObject
{
void draw() { . . . }
}
class Rectangle extends GraphicObject
{
void draw() { . . . }
}
Abstract Class :Example
29RAJESHREE KHANDE
Abstract Class
 An abstract class is not required to have an abstract
method in it.
 But any class that has an abstract method in it or that
does not provide an implementation for any abstract
methods declared in its superclasses must be
declared as an abstract class.
30RAJESHREE KHANDE

More Related Content

PPTX
Inheritance In Java
PPSX
Learn java objects inheritance-overriding-polymorphism
PPTX
Inheritance in OOPS
PPTX
inheritance
PPTX
Inheritance in java
PPTX
Inheritance in java
PPSX
Inheritance In Java
Learn java objects inheritance-overriding-polymorphism
Inheritance in OOPS
inheritance
Inheritance in java
Inheritance in java

What's hot (20)

PPTX
Inheritance In C++ (Object Oriented Programming)
PPTX
Inheritance in Object Oriented Programming
PPS
Inheritance
PPTX
Inheritance in c++
PPTX
Inheritance
PPTX
Inheritance in c++ part1
PDF
Inheritance
PPTX
Inheritance
PPTX
Inheritance
PPTX
Introduction to Inheritance
PPTX
Inheritance
PPT
Inheritance
PPT
Inheritance
PPTX
Single inheritance
PPTX
inheritance
PPTX
PDF
OOP Assign No.03(AP).pdf
PPT
Inheritance polymorphism-in-java
PDF
Demystifying oop
PPT
Inheritance in C++
Inheritance In C++ (Object Oriented Programming)
Inheritance in Object Oriented Programming
Inheritance
Inheritance in c++
Inheritance
Inheritance in c++ part1
Inheritance
Inheritance
Inheritance
Introduction to Inheritance
Inheritance
Inheritance
Inheritance
Single inheritance
inheritance
OOP Assign No.03(AP).pdf
Inheritance polymorphism-in-java
Demystifying oop
Inheritance in C++
Ad

Similar to Dr. Rajeshree Khande : Java Inheritance (20)

PPT
Inheritance and its necessity in java.ppt
PPT
Ap Power Point Chpt7
PPTX
OCA Java SE 8 Exam Chapter 5 Class Design
PPT
Unit 3 Java
PPTX
Chapter 3i
PPTX
Inheritance,single,multiple.access rulepptx
PPT
Java inheritance
PPTX
Java basics
PPT
Unit 1 Java
PPT
9781439035665 ppt ch10
PDF
Review oop and ood
DOCX
Core java by amit
PPTX
CSharp_03_Inheritance_introduction_with_examples
PPT
Chap11
PPTX
Lecture 12
DOCX
C# question answers
PPTX
Object oreinted php | OOPs
PPTX
Chapter 9 java
PPT
Chapter 5 declaring classes & oop
Inheritance and its necessity in java.ppt
Ap Power Point Chpt7
OCA Java SE 8 Exam Chapter 5 Class Design
Unit 3 Java
Chapter 3i
Inheritance,single,multiple.access rulepptx
Java inheritance
Java basics
Unit 1 Java
9781439035665 ppt ch10
Review oop and ood
Core java by amit
CSharp_03_Inheritance_introduction_with_examples
Chap11
Lecture 12
C# question answers
Object oreinted php | OOPs
Chapter 9 java
Chapter 5 declaring classes & oop
Ad

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Insiders guide to clinical Medicine.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Classroom Observation Tools for Teachers
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Pre independence Education in Inndia.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Insiders guide to clinical Medicine.pdf
O7-L3 Supply Chain Operations - ICLT Program
Classroom Observation Tools for Teachers
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Cell Types and Its function , kingdom of life
Supply Chain Operations Speaking Notes -ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Complications of Minimal Access Surgery at WLH
Sports Quiz easy sports quiz sports quiz
Pre independence Education in Inndia.pdf
01-Introduction-to-Information-Management.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial disease of the cardiovascular and lymphatic systems
102 student loan defaulters named and shamed – Is someone you know on the list?

Dr. Rajeshree Khande : Java Inheritance

  • 2. 2 Inheritance  Inheritance allows a software developer to derive a new class from an existing one.  The existing class is called the parent class or superclass  The derived class is called the child class or subclass.  Creates an is-a relationship The subclass is a more specific version of the Original. Book NovelDictionary Mystery RomanceRAJESHREE KHANDE
  • 3. Inheritance  The child class inherits the methods and data defined for the parent class  The programmer can add new variables or methods, or can modify the inherited ones.  Software reuse is at the heart of inheritance.  By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software 3RAJESHREE KHANDE
  • 4. 4 Deriving Subclasses  In Java, we use the reserved word extends to establish an inheritance relationship class Dictionary extends Book { variable declaration; method declaration; } Book NovelDictionary Mystery Romance RAJESHREE KHANDE
  • 5. Form of Inheritance Single Inheritance Hierarchical Inheritance A B A DCB 5RAJESHREE KHANDE
  • 6. Form of Inheritance B C A A B C Multilvel Inheritance Multiple Inheritance (Interface) 6RAJESHREE KHANDE
  • 7. public class Book { protected int pages = 1500; public String message() { System.out.println(“Number of pages: ” + pages); } } public class Dictionary extends Book { private int def = 52500; public void defMessage() { System.out.println(“Number of definitions” + def ); System.out.println(“Def per page: ” + (def /pages)); } } Number of pages: 1500 Number of definitions: 52500 Definitions per page: 35 7RAJESHREE KHANDE
  • 8. Some Inheritance Details  An instance of a child class does not rely on an instance of a parent class  Hence we could create a Dictionary object without having to create a Book object first  Inheritance is a one-way street  The Book class cannot use variables or methods declared explicitly in the Dictionary class 8RAJESHREE KHANDE
  • 9. 9 The protected Modifier  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 RAJESHREE KHANDE
  • 10. 10 The protected Modifier  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  the best possible encapsulation that permits inheritance RAJESHREE KHANDE
  • 11. 11 The super Reference  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. RAJESHREE KHANDE
  • 12. The super Reference  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 12RAJESHREE KHANDE
  • 13. public class Book { protected int pages; Book(int numPages) { pages = numPages; } } public class Dictionary { private int def; Dictionary(int numPages, int numDef ) { super(numPages); def = numDef ; } } 13RAJESHREE KHANDE
  • 14. Multiple Inheritance  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 14RAJESHREE KHANDE
  • 15. 15 Overriding Methods  When a child class defines a method with the same name and signature as a method in the parent class, we say that the child’s version overrides the parent’s version in favor of its own.  Signature: method’s name along with number, type, and order of its parameters  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 RAJESHREE KHANDE
  • 16. Overriding Methods  A parent method can be invoked explicitly using the super reference  If a method is declared with the final modifier, it cannot be overridden. 16RAJESHREE KHANDE
  • 17. public class Book { protected int pages; Book(int numPages) { pages = numPages; } public void message() { System.out.println(“Number of pages: ” + pages); } } public class Dictionary extends Book { protected int definitions; Dictionary(int numPages, int numDefinitions) { super(numPages); definitions = numDefinitions; } public void message() { System.out.println(“Number of definitions” + definitions); System.out.println(“Definitions per page: ” + (definitions/pages)); super.message(); } } 17RAJESHREE KHANDE
  • 18. 18 Overloading vs. Overriding  Overloading deals with multiple methods in the same class with the same name but different signatures  Overloading lets you define a similar operation in different ways for different data  Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature  Overriding lets you define a similar operation in different ways for different object types RAJESHREE KHANDE
  • 19. 19 Overloading vs. 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 RAJESHREE KHANDE
  • 20. 20 Class Hierarchies  A child class of one parent can be the parent of another child, forming a class hierarchy Book NovelDictionary Mystery Romance RAJESHREE KHANDE
  • 21. 21 The Object Class  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 RAJESHREE KHANDE
  • 22. The Object Class  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 22RAJESHREE KHANDE
  • 23. The Object Class  The equals method of the Object class returns true if two references are aliases.  We can override equals in any class to define equality in some more appropriate way  The String class (as we've seen) defines the equals method to return true if two String objects contain the same characters  Therefore the String class has overridden the equals method inherited from Object in favor of its own version 23RAJESHREE KHANDE
  • 24. Abstract Class  Sometimes, a class that you define represents an abstract concept and, should not be instantiated.  Take, for example, food in the real world. Have you ever seen an instance of food? No. What you see instead are instances of carrot, apple, and chocolate.  Food represents the abstract concept of things that we can eat. It doesn't make sense for an instance of food to exist.  Similarly in object-oriented programming, you may want to model abstract concepts but you don't want to be able to create an instance of it. 24RAJESHREE KHANDE
  • 25. Abstract Class  For example, the Number class in the java.lang package represents the abstract concept of numbers.  It makes sense to model numbers in a program, but it doesn't make sense to create a generic number object.  Instead, the Number class makes sense only as a superclass to classes like Integer and Float which implement specific kinds of numbers.  Classes such as Number, which implement abstract concepts and should not be instantiated, are called abstract classes. 25RAJESHREE KHANDE
  • 26. Abstract Class  An abstract class is a class that can only be subclassed--it cannot be instantiated.  To declare that your class is an abstract class, use the keyword abstract before the class keyword in your class declaration:  Syntax abstract class Number { . . . } If you attempt to instantiate an abstract class, the compiler will display an error similar to the following and refuse to compile your program: 26RAJESHREE KHANDE
  • 27. Abstract Method  An abstract class may contain abstract methods. i.e methods with no implementation  An abstract class can define a complete programming interface, thereby providing its subclasses with the method declarations for all of the methods necessary to implement that programming interface.  The abstract class can leave some or all of the implementation details of those methods up to its subclasses 27RAJESHREE KHANDE
  • 29. Each non-abstract subclass of GraphicObject, such as Circle and Rectangle, would have to provide an implementation for the draw method abstract class GraphicObject { int x, y; . . . abstract void draw(); } class Circle extends GraphicObject { void draw() { . . . } } class Rectangle extends GraphicObject { void draw() { . . . } } Abstract Class :Example 29RAJESHREE KHANDE
  • 30. Abstract Class  An abstract class is not required to have an abstract method in it.  But any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses must be declared as an abstract class. 30RAJESHREE KHANDE