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

What's hot

Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
Gajendra Singh Thakur
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
Ashita Agrawal
 
Inheritance
InheritanceInheritance
Inheritance
Ashish Awasthi
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 
Inheritance
InheritanceInheritance
Inheritance
SangeethaSasi1
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
Mirza Hussain
 
Inheritance
InheritanceInheritance
Inheritance
Padma Kannan
 
Inheritance
InheritanceInheritance
Inheritance
Tech_MX
 
Inheritance
InheritanceInheritance
Inheritance
Munsif Ullah
 
Introduction to Inheritance
Introduction to InheritanceIntroduction to Inheritance
Introduction to Inheritance
Keshav Vaswani
 
Inheritance
InheritanceInheritance
Inheritance
prabhat kumar
 
Inheritance
InheritanceInheritance
Inheritance
ankush_kumar
 
Inheritance
InheritanceInheritance
Inheritance
PriyankaAkhil
 
Single inheritance
Single inheritanceSingle inheritance
Single inheritance
Äkshäý M S
 
inheritance
inheritanceinheritance
inheritance
Nivetha Elangovan
 
Java
JavaJava
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
Anant240318
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
Deepak Singh
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
Alena Holligan
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 

What's hot (20)

Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Introduction to Inheritance
Introduction to InheritanceIntroduction to Inheritance
Introduction to Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Single inheritance
Single inheritanceSingle inheritance
Single inheritance
 
inheritance
inheritanceinheritance
inheritance
 
Java
JavaJava
Java
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 

Similar to Dr. Rajeshree Khande : Java Inheritance

Inheritance in java
Inheritance in javaInheritance in java
Inheritance and its necessity in java.ppt
Inheritance and its necessity in java.pptInheritance and its necessity in java.ppt
Inheritance and its necessity in java.ppt
ssuserf170c4
 
Ap Power Point Chpt7
Ap Power Point Chpt7Ap Power Point Chpt7
Ap Power Point Chpt7
dplunkett
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Design
İbrahim Kürce
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
arnold 7490
 
Chapter 3i
Chapter 3iChapter 3i
Chapter 3i
siragezeynu
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
Arati Gadgil
 
Java basics
Java basicsJava basics
Java basics
Shivanshu Purwar
 
Unit 1 Java
Unit 1 JavaUnit 1 Java
Unit 1 Java
arnold 7490
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
Terry Yoast
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and ood
than sare
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
Thakur Amit Tomer
 
Chap11
Chap11Chap11
Chap11
Terry Yoast
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
talha ijaz
 
C# question answers
C# question answersC# question answers
C# question answers
application developer
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
Ravi Bhadauria
 
Chapter 9 java
Chapter 9 javaChapter 9 java
Chapter 9 java
Ahmad sohail Kakar
 
Chapter 5 declaring classes & oop
Chapter 5 declaring classes  & oopChapter 5 declaring classes  & oop
Chapter 5 declaring classes & oop
sshhzap
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
Chamnap Chhorn
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
Nivegeetha
 

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

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance and its necessity in java.ppt
Inheritance and its necessity in java.pptInheritance and its necessity in java.ppt
Inheritance and its necessity in java.ppt
 
Ap Power Point Chpt7
Ap Power Point Chpt7Ap Power Point Chpt7
Ap Power Point Chpt7
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Design
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Chapter 3i
Chapter 3iChapter 3i
Chapter 3i
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java basics
Java basicsJava basics
Java basics
 
Unit 1 Java
Unit 1 JavaUnit 1 Java
Unit 1 Java
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and ood
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Chap11
Chap11Chap11
Chap11
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 
C# question answers
C# question answersC# question answers
C# question answers
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Chapter 9 java
Chapter 9 javaChapter 9 java
Chapter 9 java
 
Chapter 5 declaring classes & oop
Chapter 5 declaring classes  & oopChapter 5 declaring classes  & oop
Chapter 5 declaring classes & oop
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 

Recently uploaded

RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 

Recently uploaded (20)

RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 

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