SlideShare una empresa de Scribd logo
1 de 15
Descargar para leer sin conexión
Chapter 8
Inheritance
© 2004 Pearson Addison-Wesley. All rights reserved 8-2
Inheritance
• Inheritance is a fundamental object-oriented
design technique used to create and organize
reusable classes
• Chapter 8 focuses on:
 deriving new classes from existing classes
 the protected modifier
 creating class hierarchies
 indirect visibility of inherited members
 designing for inheritance
© 2004 Pearson Addison-Wesley. All rights reserved 8-3
Inheritance
• Let's say that we have designed a Java class called
Person, with some attributes and methods
• But now we want to add some extra attributes, but
not all people share these attributes, e.g.
 Bond students have a student-id, e.g. 12345678
 Bond academic staff have a position, e.g. Professor
• If we add both attributes to the existing Person
class, then all Person objects will have both a
student-id and a staff position. Not good.
• It would be nice to create new classes in Java which
inherit the attributes and methods from another
class, but which we can extend with our own stuff.
© 2004 Pearson Addison-Wesley. All rights reserved 8-4
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, 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 by the parent class
• The new class can have extra attributes, extra
methods, and it can also modify the behaviour of
the original methods.
© 2004 Pearson Addison-Wesley. All rights reserved 8-5
Inheritance
• Inheritance relationships are shown in a UML
class diagram using a solid arrow with an unfilled
triangular arrowhead pointing to the parent class
Vehicle
Car
• Proper inheritance creates an is-a relationship,
meaning the child is a more specific version of the
parent
• The Car class is more specific than the Vehicle
class. More attributes and methods can be defined
© 2004 Pearson Addison-Wesley. All rights reserved 8-6
Inheritance
• Why is inheritance useful?
• A programmer can tailor a derived class as needed
by adding new variables or methods, or by
modifying the inherited ones
• Software reuse is a fundamental benefit 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
© 2004 Pearson Addison-Wesley. All rights reserved 8-7
Creating Subclasses
• In Java, we use the reserved word extends to establish an inheritance
relationship
• See Words.java (page 440)
• See Book.java (page 441)
• See Dictionary.java (page 442)
class Car extends Vehicle
{
// class contents: new
attributes/methods
}
© 2004 Pearson Addison-Wesley. All rights reserved 8-8
The protected Modifier
• Visibility modifiers affect the way that class
members can be used in a child class
• Variables and methods declared with private
visibility cannot be referenced by name in a child
class
• They can be referenced in the child class if they
are declared with public visibility -- but public
variables violate the principle of encapsulation
• There is a third visibility modifier that helps in
inheritance situations: protected
© 2004 Pearson Addison-Wesley. All rights reserved 8-9
The protected Modifier
• The protected modifier allows a child class to
reference a variable or method directly in the child
class
• It provides more encapsulation than public
visibility, but is not as tightly encapsulated as
private visibility
• A protected variable is visible to any class in the
same package as the parent class
• Protected variables and methods can be shown
with a # symbol preceding them in UML diagrams
© 2004 Pearson Addison-Wesley. All rights reserved 8-10
Class Diagram for Words
Book
# pages : int
+ pageMessage() :
void
Dictionary
- definitions : int
+ definitionMessage() :
void
Words
+ main (args : String[]) :
void
© 2004 Pearson Addison-Wesley. All rights reserved 8-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
• See Words2.java (page 445)
• See Book2.java (page 446)
• See Dictionary2.java (page 447)
© 2004 Pearson Addison-Wesley. All rights reserved 8-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
© 2004 Pearson Addison-Wesley. All rights reserved 8-13
Overriding Methods
• 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
• See Messages.java (page 450)
• See Thought.java (page 451)
• See Advice.java (page 452)
© 2004 Pearson Addison-Wesley. All rights reserved 8-14
Class Hierarchies
• A child class of one parent can be the parent of
another child, forming a class hierarchy
Business
KMart Macys
ServiceBusiness
Kinkos
RetailBusiness
© 2004 Pearson Addison-Wesley. All rights reserved 8-15
Class Hierarchies
• Two children of the same parent are called siblings
• Common features should be put as high in the
hierarchy as is reasonable
• An inherited member is passed continually down the
line
• Therefore, a child class inherits from all its ancestor
classes
• There is no single class hierarchy that is appropriate for
all situations

Más contenido relacionado

La actualidad más candente

La actualidad más candente (18)

Cso gaddis java_chapter9ppt
Cso gaddis java_chapter9pptCso gaddis java_chapter9ppt
Cso gaddis java_chapter9ppt
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objects
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
 
INHERITANCE-Oopc ppt-ta4
INHERITANCE-Oopc ppt-ta4INHERITANCE-Oopc ppt-ta4
INHERITANCE-Oopc ppt-ta4
 
CSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - ClassesCSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - Classes
 
Basic syntax
Basic syntaxBasic syntax
Basic syntax
 
Java ppt
Java pptJava ppt
Java ppt
 
OOPS Characteristics
OOPS CharacteristicsOOPS Characteristics
OOPS Characteristics
 
Object oriented programming Fundamental Concepts
Object oriented programming Fundamental ConceptsObject oriented programming Fundamental Concepts
Object oriented programming Fundamental Concepts
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
 
27csharp
27csharp27csharp
27csharp
 
Lect1
Lect1Lect1
Lect1
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Characteristics of oop
Characteristics of oopCharacteristics of oop
Characteristics of oop
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamental
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 

Destacado

Repvblicanismvs geopoliticvs fontes origines et via massimo morigi geopolitica
Repvblicanismvs geopoliticvs fontes origines et via massimo morigi  geopoliticaRepvblicanismvs geopoliticvs fontes origines et via massimo morigi  geopolitica
Repvblicanismvs geopoliticvs fontes origines et via massimo morigi geopoliticaUNIVERSITY OF COIMBRA
 
Setup mikrotiksebagaigateway 3
Setup mikrotiksebagaigateway 3Setup mikrotiksebagaigateway 3
Setup mikrotiksebagaigateway 3shmtiger
 
The great singapore 3 d2n 1
The great singapore 3 d2n 1The great singapore 3 d2n 1
The great singapore 3 d2n 1Suky Naka
 
LCW Conceptual Restoration Workshop #6
LCW Conceptual Restoration Workshop #6LCW Conceptual Restoration Workshop #6
LCW Conceptual Restoration Workshop #6TidalInlfluence
 
I slalom recopilacion noticias
I slalom   recopilacion noticiasI slalom   recopilacion noticias
I slalom recopilacion noticiasMetalLube
 

Destacado (7)

Repvblicanismvs geopoliticvs fontes origines et via massimo morigi geopolitica
Repvblicanismvs geopoliticvs fontes origines et via massimo morigi  geopoliticaRepvblicanismvs geopoliticvs fontes origines et via massimo morigi  geopolitica
Repvblicanismvs geopoliticvs fontes origines et via massimo morigi geopolitica
 
Setup mikrotiksebagaigateway 3
Setup mikrotiksebagaigateway 3Setup mikrotiksebagaigateway 3
Setup mikrotiksebagaigateway 3
 
El ziggurat d'Eridú grup 2
El ziggurat d'Eridú grup 2El ziggurat d'Eridú grup 2
El ziggurat d'Eridú grup 2
 
The great singapore 3 d2n 1
The great singapore 3 d2n 1The great singapore 3 d2n 1
The great singapore 3 d2n 1
 
Japò
JapòJapò
Japò
 
LCW Conceptual Restoration Workshop #6
LCW Conceptual Restoration Workshop #6LCW Conceptual Restoration Workshop #6
LCW Conceptual Restoration Workshop #6
 
I slalom recopilacion noticias
I slalom   recopilacion noticiasI slalom   recopilacion noticias
I slalom recopilacion noticias
 

Similar a Inheritance

Chapter08 - Inheritance.ppt
Chapter08 - Inheritance.pptChapter08 - Inheritance.ppt
Chapter08 - Inheritance.pptPatrick Okot
 
Lecture 5 inheritance
Lecture 5 inheritanceLecture 5 inheritance
Lecture 5 inheritancethe_wumberlog
 
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.pptssuserf170c4
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingVasilios Kuznos
 
Ap Power Point Chpt7
Ap Power Point Chpt7Ap Power Point Chpt7
Ap Power Point Chpt7dplunkett
 
Chapter 4 Absolute Java
Chapter 4 Absolute Java Chapter 4 Absolute Java
Chapter 4 Absolute Java Shariq Alee
 
Java htp6e 09
Java htp6e 09Java htp6e 09
Java htp6e 09Ayesha ch
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Top 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersTop 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersWhizlabs
 
Inheritance in oop
Inheritance in oopInheritance in oop
Inheritance in oopMuskanNazeer
 
Polymorphism
PolymorphismPolymorphism
PolymorphismNuha Noor
 

Similar a Inheritance (20)

Chapter08 - Inheritance.ppt
Chapter08 - Inheritance.pptChapter08 - Inheritance.ppt
Chapter08 - Inheritance.ppt
 
Lecture 5 inheritance
Lecture 5 inheritanceLecture 5 inheritance
Lecture 5 inheritance
 
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
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Ap Power Point Chpt7
Ap Power Point Chpt7Ap Power Point Chpt7
Ap Power Point Chpt7
 
Chapter 4 Absolute Java
Chapter 4 Absolute Java Chapter 4 Absolute Java
Chapter 4 Absolute Java
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Chap4java5th
Chap4java5thChap4java5th
Chap4java5th
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
 
Java htp6e 09
Java htp6e 09Java htp6e 09
Java htp6e 09
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersTop 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed Answers
 
Inheritance in oop
Inheritance in oopInheritance in oop
Inheritance in oop
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Más de hccit

Snr ipt 10_syll
Snr ipt 10_syllSnr ipt 10_syll
Snr ipt 10_syllhccit
 
Snr ipt 10_guide
Snr ipt 10_guideSnr ipt 10_guide
Snr ipt 10_guidehccit
 
3 d modelling_task_sheet_2014_yr12
3 d modelling_task_sheet_2014_yr123 d modelling_task_sheet_2014_yr12
3 d modelling_task_sheet_2014_yr12hccit
 
3 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr113 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr11hccit
 
10 ict photoshop_proj_2014
10 ict photoshop_proj_201410 ict photoshop_proj_2014
10 ict photoshop_proj_2014hccit
 
Photoshop
PhotoshopPhotoshop
Photoshophccit
 
Flash
FlashFlash
Flashhccit
 
University partnerships programs email
University partnerships programs emailUniversity partnerships programs email
University partnerships programs emailhccit
 
Griffith sciences pathway programs overview
Griffith sciences pathway programs overviewGriffith sciences pathway programs overview
Griffith sciences pathway programs overviewhccit
 
Griffith info tech brochure
Griffith info tech brochureGriffith info tech brochure
Griffith info tech brochurehccit
 
Pm sql exercises
Pm sql exercisesPm sql exercises
Pm sql exerciseshccit
 
Repairs questions
Repairs questionsRepairs questions
Repairs questionshccit
 
Movies questions
Movies questionsMovies questions
Movies questionshccit
 
Australian birds questions
Australian birds questionsAustralian birds questions
Australian birds questionshccit
 
Section b
Section bSection b
Section bhccit
 
Section a
Section aSection a
Section ahccit
 
Ask manual rev5
Ask manual rev5Ask manual rev5
Ask manual rev5hccit
 
Case study report mj
Case study report mjCase study report mj
Case study report mjhccit
 

Más de hccit (20)

Snr ipt 10_syll
Snr ipt 10_syllSnr ipt 10_syll
Snr ipt 10_syll
 
Snr ipt 10_guide
Snr ipt 10_guideSnr ipt 10_guide
Snr ipt 10_guide
 
3 d modelling_task_sheet_2014_yr12
3 d modelling_task_sheet_2014_yr123 d modelling_task_sheet_2014_yr12
3 d modelling_task_sheet_2014_yr12
 
3 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr113 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr11
 
10 ict photoshop_proj_2014
10 ict photoshop_proj_201410 ict photoshop_proj_2014
10 ict photoshop_proj_2014
 
Photoshop
PhotoshopPhotoshop
Photoshop
 
Flash
FlashFlash
Flash
 
University partnerships programs email
University partnerships programs emailUniversity partnerships programs email
University partnerships programs email
 
Griffith sciences pathway programs overview
Griffith sciences pathway programs overviewGriffith sciences pathway programs overview
Griffith sciences pathway programs overview
 
Griffith info tech brochure
Griffith info tech brochureGriffith info tech brochure
Griffith info tech brochure
 
Pm sql exercises
Pm sql exercisesPm sql exercises
Pm sql exercises
 
Repairs questions
Repairs questionsRepairs questions
Repairs questions
 
Movies questions
Movies questionsMovies questions
Movies questions
 
Australian birds questions
Australian birds questionsAustralian birds questions
Australian birds questions
 
Section b
Section bSection b
Section b
 
B
BB
B
 
A
AA
A
 
Section a
Section aSection a
Section a
 
Ask manual rev5
Ask manual rev5Ask manual rev5
Ask manual rev5
 
Case study report mj
Case study report mjCase study report mj
Case study report mj
 

Último

SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTSIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTxtailishbaloch
 
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through TokenizationStobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through TokenizationStobox
 
Flow Control | Block Size | ST Min | First Frame
Flow Control | Block Size | ST Min | First FrameFlow Control | Block Size | ST Min | First Frame
Flow Control | Block Size | ST Min | First FrameKapil Thakar
 
EMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? WebinarEMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? WebinarThousandEyes
 
How to release an Open Source Dataweave Library
How to release an Open Source Dataweave LibraryHow to release an Open Source Dataweave Library
How to release an Open Source Dataweave Libraryshyamraj55
 
Patch notes explaining DISARM Version 1.4 update
Patch notes explaining DISARM Version 1.4 updatePatch notes explaining DISARM Version 1.4 update
Patch notes explaining DISARM Version 1.4 updateadam112203
 
Introduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its applicationIntroduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its applicationKnoldus Inc.
 
.NET 8 ChatBot with Azure OpenAI Services.pptx
.NET 8 ChatBot with Azure OpenAI Services.pptx.NET 8 ChatBot with Azure OpenAI Services.pptx
.NET 8 ChatBot with Azure OpenAI Services.pptxHansamali Gamage
 
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptxGraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptxNeo4j
 
Automation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsAutomation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsDianaGray10
 
Top 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTop 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTopCSSGallery
 
IT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced ComputingIT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced ComputingMAGNIntelligence
 
Technical SEO for Improved Accessibility WTS FEST
Technical SEO for Improved Accessibility  WTS FESTTechnical SEO for Improved Accessibility  WTS FEST
Technical SEO for Improved Accessibility WTS FESTBillieHyde
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
 
UiPath Studio Web workshop series - Day 4
UiPath Studio Web workshop series - Day 4UiPath Studio Web workshop series - Day 4
UiPath Studio Web workshop series - Day 4DianaGray10
 
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInThousandEyes
 
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series  - Day 1UiPath Studio Web workshop series  - Day 1
UiPath Studio Web workshop series - Day 1DianaGray10
 
Novo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNovo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNeo4j
 
Graphene Quantum Dots-Based Composites for Biomedical Applications
Graphene Quantum Dots-Based Composites for  Biomedical ApplicationsGraphene Quantum Dots-Based Composites for  Biomedical Applications
Graphene Quantum Dots-Based Composites for Biomedical Applicationsnooralam814309
 

Último (20)

SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTSIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
 
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through TokenizationStobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
 
Flow Control | Block Size | ST Min | First Frame
Flow Control | Block Size | ST Min | First FrameFlow Control | Block Size | ST Min | First Frame
Flow Control | Block Size | ST Min | First Frame
 
EMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? WebinarEMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? Webinar
 
How to release an Open Source Dataweave Library
How to release an Open Source Dataweave LibraryHow to release an Open Source Dataweave Library
How to release an Open Source Dataweave Library
 
Patch notes explaining DISARM Version 1.4 update
Patch notes explaining DISARM Version 1.4 updatePatch notes explaining DISARM Version 1.4 update
Patch notes explaining DISARM Version 1.4 update
 
Introduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its applicationIntroduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its application
 
.NET 8 ChatBot with Azure OpenAI Services.pptx
.NET 8 ChatBot with Azure OpenAI Services.pptx.NET 8 ChatBot with Azure OpenAI Services.pptx
.NET 8 ChatBot with Azure OpenAI Services.pptx
 
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptxGraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
 
Automation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsAutomation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projects
 
Top 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTop 10 Squarespace Development Companies
Top 10 Squarespace Development Companies
 
IT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced ComputingIT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced Computing
 
Technical SEO for Improved Accessibility WTS FEST
Technical SEO for Improved Accessibility  WTS FESTTechnical SEO for Improved Accessibility  WTS FEST
Technical SEO for Improved Accessibility WTS FEST
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
UiPath Studio Web workshop series - Day 4
UiPath Studio Web workshop series - Day 4UiPath Studio Web workshop series - Day 4
UiPath Studio Web workshop series - Day 4
 
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
 
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series  - Day 1UiPath Studio Web workshop series  - Day 1
UiPath Studio Web workshop series - Day 1
 
Novo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNovo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4j
 
Graphene Quantum Dots-Based Composites for Biomedical Applications
Graphene Quantum Dots-Based Composites for  Biomedical ApplicationsGraphene Quantum Dots-Based Composites for  Biomedical Applications
Graphene Quantum Dots-Based Composites for Biomedical Applications
 

Inheritance

  • 2. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance • Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes • Chapter 8 focuses on:  deriving new classes from existing classes  the protected modifier  creating class hierarchies  indirect visibility of inherited members  designing for inheritance
  • 3. © 2004 Pearson Addison-Wesley. All rights reserved 8-3 Inheritance • Let's say that we have designed a Java class called Person, with some attributes and methods • But now we want to add some extra attributes, but not all people share these attributes, e.g.  Bond students have a student-id, e.g. 12345678  Bond academic staff have a position, e.g. Professor • If we add both attributes to the existing Person class, then all Person objects will have both a student-id and a staff position. Not good. • It would be nice to create new classes in Java which inherit the attributes and methods from another class, but which we can extend with our own stuff.
  • 4. © 2004 Pearson Addison-Wesley. All rights reserved 8-4 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, 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 by the parent class • The new class can have extra attributes, extra methods, and it can also modify the behaviour of the original methods.
  • 5. © 2004 Pearson Addison-Wesley. All rights reserved 8-5 Inheritance • Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class Vehicle Car • Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent • The Car class is more specific than the Vehicle class. More attributes and methods can be defined
  • 6. © 2004 Pearson Addison-Wesley. All rights reserved 8-6 Inheritance • Why is inheritance useful? • A programmer can tailor a derived class as needed by adding new variables or methods, or by modifying the inherited ones • Software reuse is a fundamental benefit 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
  • 7. © 2004 Pearson Addison-Wesley. All rights reserved 8-7 Creating Subclasses • In Java, we use the reserved word extends to establish an inheritance relationship • See Words.java (page 440) • See Book.java (page 441) • See Dictionary.java (page 442) class Car extends Vehicle { // class contents: new attributes/methods }
  • 8. © 2004 Pearson Addison-Wesley. All rights reserved 8-8 The protected Modifier • Visibility modifiers affect the way that class members can be used in a child class • Variables and methods declared with private visibility cannot be referenced by name in a child class • They can be referenced in the child class if they are declared with public visibility -- but public variables violate the principle of encapsulation • There is a third visibility modifier that helps in inheritance situations: protected
  • 9. © 2004 Pearson Addison-Wesley. All rights reserved 8-9 The protected Modifier • The protected modifier allows a child class to reference a variable or method directly in the child class • It provides more encapsulation than public visibility, but is not as tightly encapsulated as private visibility • A protected variable is visible to any class in the same package as the parent class • Protected variables and methods can be shown with a # symbol preceding them in UML diagrams
  • 10. © 2004 Pearson Addison-Wesley. All rights reserved 8-10 Class Diagram for Words Book # pages : int + pageMessage() : void Dictionary - definitions : int + definitionMessage() : void Words + main (args : String[]) : void
  • 11. © 2004 Pearson Addison-Wesley. All rights reserved 8-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 • See Words2.java (page 445) • See Book2.java (page 446) • See Dictionary2.java (page 447)
  • 12. © 2004 Pearson Addison-Wesley. All rights reserved 8-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
  • 13. © 2004 Pearson Addison-Wesley. All rights reserved 8-13 Overriding Methods • 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 • See Messages.java (page 450) • See Thought.java (page 451) • See Advice.java (page 452)
  • 14. © 2004 Pearson Addison-Wesley. All rights reserved 8-14 Class Hierarchies • A child class of one parent can be the parent of another child, forming a class hierarchy Business KMart Macys ServiceBusiness Kinkos RetailBusiness
  • 15. © 2004 Pearson Addison-Wesley. All rights reserved 8-15 Class Hierarchies • Two children of the same parent are called siblings • Common features should be put as high in the hierarchy as is reasonable • An inherited member is passed continually down the line • Therefore, a child class inherits from all its ancestor classes • There is no single class hierarchy that is appropriate for all situations