SlideShare a Scribd company logo
1 of 15
Download to read offline
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

More Related Content

What's hot (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
 

Viewers also liked

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
 

Viewers also liked (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 to 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 to 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
 

More from 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
 

More from 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
 

Recently uploaded

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Recently uploaded (20)

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

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