SlideShare a Scribd company logo
Inheritance
61
Java Programming: OOP
62
Inheritance
• On the surface, inheritance is a code re-use issue.
– we can extend code that is already written in a
manageable manner.
• Inheritance is more, it supports polymorphism at the
language level!
• Information is made manageable in a hierarchical
order
Inheritance cont.
• With Inheritance, new class can be derived from
existing classes as a building block
• New class Inherit properties and methods from the
existing class
• New class can also added Its own properties and
methods
• Keyword
– Extends
– Implements
63
Java Programming: OOP
64
Inheritance cont.
• Take an existing object type (collection of fields and
methods) and extend it.
– create a special version of the code without re-
writing any of the existing code (or even explicitly
calling it!).
– End result is a more specific object type, called
the sub-class / derived class / child class.
– The original code is called the super class /
parent class / base class.
Example
7/11/2015 Budditha Hettige (budditha@yahoo.com) 65
Student
University
Student
Undergraduate Post graduate
Employee
Phone
Employee
-----------------
-
Name
Email
Phone
FulltimeEmployee
------------------------
Salary
Office
Manager
Manager
------------------
CompanyCar
Email
Phone
Contractor
------------------
HourlyRate,
ContractDuration
66
Introduction (Cont.)
• Class hierarchy
– Direct superclass
• Inherited explicitly (one level up hierarchy)
– Indirect superclass
• Inherited two or more levels up hierarchy
– Single inheritance
• Inherits from one superclass
– Multiple inheritance
• Inherits from multiple superclasses
Inheritance
67
Animal
Dog
public class Animal
{
}
public class Dog extends Animal
{ }
Introduction (Cont.)
68
Animal
Dog
Animal
Dog
Private
Public
Public
69
protected
protected
protected
protected Members
• protected access
– Intermediate level of protection between public
and private
– protected members accessible by
• superclass members
• subclass members
• Class members in the same package
– Subclass access to superclass member
• Keyword super and a dot (.)
Introduction (Cont.)
70
Animal
Dog
Animal
Dog
Private
Public
Public
Protected
Protected
Example
71
IS-A Relationship (Example)
public class Animal
{
}
public class Mammal extends Animal
{ }
public class Reptile extends Animal
{ }
public class Dog extends Animal
{ }
72
Animal
Mamal
extends
IS-A Relationship (Example)
public class Dog extends Mammal
{
public static void main(String args[])
{
Animal a = new Animal();
Reptile r = new Reptile();
Mammal m = new Mammal();
Dog d = new Dog();
System.out.println(m instanceof Animal);
System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}
73
Animal
Mamal Dog Reptile
IS-A Relationship
• In Object Oriented terms following are true:
– Animal is the superclass of Mammal class.
– Animal is the superclass of Reptile class.
– Mammal and Reptile are sub classes of Animal
class.
– Dog is the subclass of both Mammal and Animal
classes.
74
IS-A Relationship
• IS-A relationship we can say:
– Mammal IS-A Animal
– Reptile IS-A Animal
– Dog IS-A Mammal
– Hence : Dog IS-A Animal as well
• Use of the extends keyword the subclasses will be
able to inherit all the properties of the superclass
except for the private properties of the superclass
75
Java Programming: OOP
76
Accessing super class methods
• Can use super() to access all (non-private) super
class methods.
– even those replaced with new versions in the
derived class.
• Can use super() to call base class constructor.
Java Programming: OOP
77
Single Inheritance
• You can't extend more than one class!
– the derived class can't have more than one base
class.
• You can do multiple inheritance with interface
inheritance.
Java Programming: OOP
78
Inheritance Example cont.
• Employee: name, email, phone
– FulltimeEmployee: also has salary, office, benefits, …
• Manager: CompanyCar, can change salaries, rates contracts,
offices, etc.
– Contractor: HourlyRate, ContractDuration, …
• A manager a special kind of FullTimeEmployee,
which is a special kind of Employee.
• The relationship modeled by inheritance is often
referred to as a “is a” relationship
Inheritance Example
79
Employee
------------------
Name
Email
Phone
FulltimeEmployee
------------------------
Salary
Office
Manager
Manager
------------------
CompanyCar
Email
Phone
Contractor
------------------
HourlyRate,
ContractDuration
Inheritance Example
80
Employee
------------------
Name
Email
Phone
FulltimeEmployee
------------------------
Salary
Office
Manager
Manager
------------------
CompanyCar
Email
Phone
Contractor
------------------
HourlyRate,
ContractDuration
Is a
Is a Is a
Example
7/11/2015 Budditha Hettige (budditha@yahoo.com) 81
Example
7/11/2015 Budditha Hettige (budditha@yahoo.com) 82
Example
7/11/2015 Budditha Hettige (budditha@yahoo.com) 83
Ad

More Related Content

Similar to 03-inheretance java code programming .pdf (20)

Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
mcollison
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Lovely Professional University
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
DevaKumari Vijay
 
Inheritance in oop
Inheritance in oopInheritance in oop
Inheritance in oop
MuskanNazeer
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
Jyothishmathi Institute of Technology and Science Karimnagar
 
Java
JavaJava
Java
NAIM PARVEZ GALIB
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptxInheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
NITHISG1
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
123 JAVA CLASSES, OBJECTS AND METHODS.ppt123 JAVA CLASSES, OBJECTS AND METHODS.ppt
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
mcjaya2024
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Java htp6e 09
Java htp6e 09Java htp6e 09
Java htp6e 09
Ayesha ch
 
object oriented programming unit two ppt
object oriented programming unit two pptobject oriented programming unit two ppt
object oriented programming unit two ppt
isiagnel2
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
Rosie Jane Enomar
 
javainheritance
javainheritancejavainheritance
javainheritance
Arjun Shanka
 
Inheritance
InheritanceInheritance
Inheritance
Munsif Ullah
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
teach4uin
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
teach4uin
 
Java - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxJava - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
mcollison
 
Inheritance in oop
Inheritance in oopInheritance in oop
Inheritance in oop
MuskanNazeer
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptxInheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
NITHISG1
 
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
123 JAVA CLASSES, OBJECTS AND METHODS.ppt123 JAVA CLASSES, OBJECTS AND METHODS.ppt
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
mcjaya2024
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Java htp6e 09
Java htp6e 09Java htp6e 09
Java htp6e 09
Ayesha ch
 
object oriented programming unit two ppt
object oriented programming unit two pptobject oriented programming unit two ppt
object oriented programming unit two ppt
isiagnel2
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
teach4uin
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
teach4uin
 
Java - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxJava - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 

More from sithumMarasighe (10)

2020-rules-of-differentiation-sinhala.pdf
2020-rules-of-differentiation-sinhala.pdf2020-rules-of-differentiation-sinhala.pdf
2020-rules-of-differentiation-sinhala.pdf
sithumMarasighe
 
project management cpm and peat digram .pdf
project management   cpm and peat digram .pdfproject management   cpm and peat digram .pdf
project management cpm and peat digram .pdf
sithumMarasighe
 
02-advance-methods-on-class jdpgs code .pdf
02-advance-methods-on-class jdpgs code .pdf02-advance-methods-on-class jdpgs code .pdf
02-advance-methods-on-class jdpgs code .pdf
sithumMarasighe
 
oop_full java coding and esay to understand.pdf
oop_full java coding  and  esay to  understand.pdfoop_full java coding  and  esay to  understand.pdf
oop_full java coding and esay to understand.pdf
sithumMarasighe
 
01-class-and-objects java code in the .pdf
01-class-and-objects  java code  in the .pdf01-class-and-objects  java code  in the .pdf
01-class-and-objects java code in the .pdf
sithumMarasighe
 
04_-note voerrding java code simapl-oop.pdf
04_-note  voerrding java code  simapl-oop.pdf04_-note  voerrding java code  simapl-oop.pdf
04_-note voerrding java code simapl-oop.pdf
sithumMarasighe
 
AI for Sustainable Agriculture______.pdf
AI for Sustainable Agriculture______.pdfAI for Sustainable Agriculture______.pdf
AI for Sustainable Agriculture______.pdf
sithumMarasighe
 
Tree Guide March 2021 .pdf
Tree Guide March 2021               .pdfTree Guide March 2021               .pdf
Tree Guide March 2021 .pdf
sithumMarasighe
 
CSS & PHP10101010110101010101010100.pdf
CSS  & PHP10101010110101010101010100.pdfCSS  & PHP10101010110101010101010100.pdf
CSS & PHP10101010110101010101010100.pdf
sithumMarasighe
 
Apple vs. Fortnite The Legal Battle.pptx
Apple vs. Fortnite The Legal Battle.pptxApple vs. Fortnite The Legal Battle.pptx
Apple vs. Fortnite The Legal Battle.pptx
sithumMarasighe
 
2020-rules-of-differentiation-sinhala.pdf
2020-rules-of-differentiation-sinhala.pdf2020-rules-of-differentiation-sinhala.pdf
2020-rules-of-differentiation-sinhala.pdf
sithumMarasighe
 
project management cpm and peat digram .pdf
project management   cpm and peat digram .pdfproject management   cpm and peat digram .pdf
project management cpm and peat digram .pdf
sithumMarasighe
 
02-advance-methods-on-class jdpgs code .pdf
02-advance-methods-on-class jdpgs code .pdf02-advance-methods-on-class jdpgs code .pdf
02-advance-methods-on-class jdpgs code .pdf
sithumMarasighe
 
oop_full java coding and esay to understand.pdf
oop_full java coding  and  esay to  understand.pdfoop_full java coding  and  esay to  understand.pdf
oop_full java coding and esay to understand.pdf
sithumMarasighe
 
01-class-and-objects java code in the .pdf
01-class-and-objects  java code  in the .pdf01-class-and-objects  java code  in the .pdf
01-class-and-objects java code in the .pdf
sithumMarasighe
 
04_-note voerrding java code simapl-oop.pdf
04_-note  voerrding java code  simapl-oop.pdf04_-note  voerrding java code  simapl-oop.pdf
04_-note voerrding java code simapl-oop.pdf
sithumMarasighe
 
AI for Sustainable Agriculture______.pdf
AI for Sustainable Agriculture______.pdfAI for Sustainable Agriculture______.pdf
AI for Sustainable Agriculture______.pdf
sithumMarasighe
 
Tree Guide March 2021 .pdf
Tree Guide March 2021               .pdfTree Guide March 2021               .pdf
Tree Guide March 2021 .pdf
sithumMarasighe
 
CSS & PHP10101010110101010101010100.pdf
CSS  & PHP10101010110101010101010100.pdfCSS  & PHP10101010110101010101010100.pdf
CSS & PHP10101010110101010101010100.pdf
sithumMarasighe
 
Apple vs. Fortnite The Legal Battle.pptx
Apple vs. Fortnite The Legal Battle.pptxApple vs. Fortnite The Legal Battle.pptx
Apple vs. Fortnite The Legal Battle.pptx
sithumMarasighe
 
Ad

Recently uploaded (20)

PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdfPRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Guru
 
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
IJCNCJournal
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
Understanding Structural Loads and Load Paths
Understanding Structural Loads and Load PathsUnderstanding Structural Loads and Load Paths
Understanding Structural Loads and Load Paths
University of Kirkuk
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Redirects Unraveled: From Lost Links to Rickrolls
Redirects Unraveled: From Lost Links to RickrollsRedirects Unraveled: From Lost Links to Rickrolls
Redirects Unraveled: From Lost Links to Rickrolls
Kritika Garg
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1
remoteaimms
 
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Journal of Soft Computing in Civil Engineering
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Journal of Soft Computing in Civil Engineering
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdfPRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Guru
 
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
IJCNCJournal
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
Understanding Structural Loads and Load Paths
Understanding Structural Loads and Load PathsUnderstanding Structural Loads and Load Paths
Understanding Structural Loads and Load Paths
University of Kirkuk
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Redirects Unraveled: From Lost Links to Rickrolls
Redirects Unraveled: From Lost Links to RickrollsRedirects Unraveled: From Lost Links to Rickrolls
Redirects Unraveled: From Lost Links to Rickrolls
Kritika Garg
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1
remoteaimms
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Ad

03-inheretance java code programming .pdf

  翻译: