SlideShare une entreprise Scribd logo
1  sur  30
1 Chapter 05: Polymorphism
Overriding Superclass Methods 2 Create subclass by extending existing class Subclass contains data and methods defined in original superclass Sometimes superclass data fields and methods not entirely appropriate for subclass objects Polymorphism Using same method name to indicate different implementations
Overriding Superclass Methods (continued) 3 Override methodin parent class Create method in child class that has same name and argument list as method in parent class Subtype polymorphism Ability of one method name to work appropriately for different subclass objects of same parent class
Understanding How ConstructorsAre Called During Inheritance 4 Instantiate object that is member of subclass  Call at least two constructors Constructor for base class  Constructor for derived class Superclass constructor must execute first When superclass contains default constructor Execution of superclass constructor transparent
5
Understanding How ConstructorsAre Called During Inheritance (continued) 6
Using Superclass Constructorsthat Require Arguments 7 When you write your own constructor You replace automatically supplied version When extending superclass with constructors that require arguments Subclass must provide superclass constructor with arguments it needs
Using Superclass Constructorsthat Require Arguments (continued) 8 When superclass has default constructor Can create subclass with or without own constructor When superclass contains only constructors that require arguments Must include at least one constructor for each subclass you create First statement within each constructor must call superclass constructor
Using Superclass Constructorsthat Require Arguments (continued) 9 Call superclass constructor super(list of arguments); Keyword super Always refers to superclass
Accessing Superclass Methods 10 Use overridden superclass method within subclass Use keyword super to access parent class method Comparing this and super Think of the keyword this as the opposite of super  Within a subclass When parent class contains a method that is not overridden Child can use the method name with super, this, or alone
Accessing Superclass Methods (continued) 11
Quick Quiz 12 1.	Using the same method name to indicate different implementations is called ____.  2. True or False: When you instantiate an object that is a member of a subclass, you are actually calling at least two constructors.  3. The keyword ____ always refers to the superclass of the class in which you use it.
Learning About Information Hiding 13 Student class Keyword private precedes each data field Keyword public precedes each method Information hiding Concept of keeping data private Data can be altered only by methods you choose and only in ways that you can control
14
Learning About Information Hiding (continued) 15 When class serves as superclass  Subclasses inherit all data and methods of superclass Except private members of parent class not accessible within child class’s methods
Learning About Information Hiding (continued) 16 Keyword protected Provides intermediate level of security between public and private access Can be used within own class or in any classes extended from that class Cannot be used by “outside” classes
Methods You Cannot Override 17 static methods final methods Methods within final classes
A Subclass Cannot Override staticMethods in Its Superclass 18 Subclass cannot override methods declared static in superclass Can hidestatic method in superclass By declaring static method with same signature as static method in superclass Call new static method from within subclass or in another class by using subclass object Within static method of subclass Cannot access parent method using super object
A Subclass Cannot Override staticMethods in Its Superclass (continued) 19 Although child class cannot inherit parent’s static methods Can access parent’s static methods in the same way any other class can
A Subclass Cannot Override staticMethods in Its Superclass (continued) 20
A Subclass Cannot Override finalMethods in Its Superclass 21 Subclass cannot override methods declared final in superclass final modifier Does not allow method to be overridden Virtual method calls Default in Java Method used is determined when program runs  Type of object used might not be known until method executes
A Subclass Cannot Override finalMethods in Its Superclass (continued) 22 Advantage to making method final Compiler knows there is only one version of method Compiler knows which method version will be used Can optimize program’s performance  By removing calls to final methods  Replacing them with expanded code of their definitions  At each method call location Called inlining
A Subclass Cannot Override Methodsin a final Superclass 23 Declare class final All of its methods are final Regardless of which access modifier precedes method name Cannot be a parent class
A Subclass Cannot Override Methodsin a final Superclass (continued) 24
You Do It 25 Creating a superclass and an application to use it Creating a subclass and an application to use it Creating a subclass method that overrides a superclass method Understanding the role of constructors in inheritance
You Do It (continued) 26 Inheritance when the superclass requires constructor arguments Accessing an overridden superclass method from within a subclass
Don’t Do It 27 Don’t capitalize the “o” in the instanceof operator Don’t try to directly access private superclass members from a subclass Don’t forget to call a superclass constructor from within a subclass constructor if the superclass does not contain a default constructor
Summary 28 Inheritance Mechanism that enables one class to inherit both behavior and attributes of another class Keyword extends Achieve inheritance in Java Polymorphism  Act of using same method name to indicate different implementations
Summary (continued) 29 Use a superclass method within a subclass Use keyword super to access it Information hiding Concept of keeping data private Keyword protected Intermediate level of security between public and private access Subclass cannot override methods  Declared static in superclass Declared final or class final
30 Quick Quiz 3  The concept of keeping data private is known as ____.  2. Using the keyword ____ provides you with an intermediate level of security between public and private access.  3. True or False: Although a child class cannot inherit its parent’s static methods, it can access its parent’s static methods the same way any other class can.  4. You can declare a class to be ____. When you do, all of its methods are final, regardless of which access modifier precedes the method name.

Contenu connexe

Tendances

Chapter 5 declaring classes & oop
Chapter 5 declaring classes  & oopChapter 5 declaring classes  & oop
Chapter 5 declaring classes & oopsshhzap
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 eehomeworkping9
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access SpecifierDeeptiJava
 
Mca 2nd sem u-3 inheritance
Mca 2nd  sem u-3 inheritanceMca 2nd  sem u-3 inheritance
Mca 2nd sem u-3 inheritanceRai University
 
Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13Vince Vo
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in javaHrithikShinde
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core ParcticalGaurav Mehta
 
Bca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritanceBca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritanceRai University
 
Top 20 c# interview Question and answers
Top 20 c# interview Question and answersTop 20 c# interview Question and answers
Top 20 c# interview Question and answersw3asp dotnet
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#Abid Kohistani
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdmHarshal Misalkar
 

Tendances (20)

Chapter 5 declaring classes & oop
Chapter 5 declaring classes  & oopChapter 5 declaring classes  & oop
Chapter 5 declaring classes & oop
 
C#
C#C#
C#
 
Hemajava
HemajavaHemajava
Hemajava
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
Mixin it up
Mixin it upMixin it up
Mixin it up
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access Specifier
 
Mca 2nd sem u-3 inheritance
Mca 2nd  sem u-3 inheritanceMca 2nd  sem u-3 inheritance
Mca 2nd sem u-3 inheritance
 
14 interface
14  interface14  interface
14 interface
 
Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
 
Simple java program
Simple java programSimple java program
Simple java program
 
Types of methods in python
Types of methods in pythonTypes of methods in python
Types of methods in python
 
Bca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritanceBca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritance
 
Top 20 c# interview Question and answers
Top 20 c# interview Question and answersTop 20 c# interview Question and answers
Top 20 c# interview Question and answers
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
Chap02
Chap02Chap02
Chap02
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Java interface
Java interfaceJava interface
Java interface
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 

Similaire à Chapter 05 polymorphism extra

Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance ConceptsVicter Paul
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism JavaM. Raihan
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritanceraksharao
 
Polymorphism
PolymorphismPolymorphism
PolymorphismKumar
 
Java inheritance
Java inheritanceJava inheritance
Java inheritanceSmrutiShah9
 
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 overridingNithyaN19
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-javaDeepak Singh
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2sotlsoc
 
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
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview QestionsArun Vasanth
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingWondimuBantihun1
 

Similaire à Chapter 05 polymorphism extra (20)

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Chapter 3i
Chapter 3iChapter 3i
Chapter 3i
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
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
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
javainheritance
javainheritancejavainheritance
javainheritance
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
 
Java presentation
Java presentationJava presentation
Java presentation
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
 
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
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programming
 

Dernier

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 

Dernier (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

Chapter 05 polymorphism extra

  • 1. 1 Chapter 05: Polymorphism
  • 2. Overriding Superclass Methods 2 Create subclass by extending existing class Subclass contains data and methods defined in original superclass Sometimes superclass data fields and methods not entirely appropriate for subclass objects Polymorphism Using same method name to indicate different implementations
  • 3. Overriding Superclass Methods (continued) 3 Override methodin parent class Create method in child class that has same name and argument list as method in parent class Subtype polymorphism Ability of one method name to work appropriately for different subclass objects of same parent class
  • 4. Understanding How ConstructorsAre Called During Inheritance 4 Instantiate object that is member of subclass Call at least two constructors Constructor for base class Constructor for derived class Superclass constructor must execute first When superclass contains default constructor Execution of superclass constructor transparent
  • 5. 5
  • 6. Understanding How ConstructorsAre Called During Inheritance (continued) 6
  • 7. Using Superclass Constructorsthat Require Arguments 7 When you write your own constructor You replace automatically supplied version When extending superclass with constructors that require arguments Subclass must provide superclass constructor with arguments it needs
  • 8. Using Superclass Constructorsthat Require Arguments (continued) 8 When superclass has default constructor Can create subclass with or without own constructor When superclass contains only constructors that require arguments Must include at least one constructor for each subclass you create First statement within each constructor must call superclass constructor
  • 9. Using Superclass Constructorsthat Require Arguments (continued) 9 Call superclass constructor super(list of arguments); Keyword super Always refers to superclass
  • 10. Accessing Superclass Methods 10 Use overridden superclass method within subclass Use keyword super to access parent class method Comparing this and super Think of the keyword this as the opposite of super Within a subclass When parent class contains a method that is not overridden Child can use the method name with super, this, or alone
  • 12. Quick Quiz 12 1. Using the same method name to indicate different implementations is called ____. 2. True or False: When you instantiate an object that is a member of a subclass, you are actually calling at least two constructors. 3. The keyword ____ always refers to the superclass of the class in which you use it.
  • 13. Learning About Information Hiding 13 Student class Keyword private precedes each data field Keyword public precedes each method Information hiding Concept of keeping data private Data can be altered only by methods you choose and only in ways that you can control
  • 14. 14
  • 15. Learning About Information Hiding (continued) 15 When class serves as superclass Subclasses inherit all data and methods of superclass Except private members of parent class not accessible within child class’s methods
  • 16. Learning About Information Hiding (continued) 16 Keyword protected Provides intermediate level of security between public and private access Can be used within own class or in any classes extended from that class Cannot be used by “outside” classes
  • 17. Methods You Cannot Override 17 static methods final methods Methods within final classes
  • 18. A Subclass Cannot Override staticMethods in Its Superclass 18 Subclass cannot override methods declared static in superclass Can hidestatic method in superclass By declaring static method with same signature as static method in superclass Call new static method from within subclass or in another class by using subclass object Within static method of subclass Cannot access parent method using super object
  • 19. A Subclass Cannot Override staticMethods in Its Superclass (continued) 19 Although child class cannot inherit parent’s static methods Can access parent’s static methods in the same way any other class can
  • 20. A Subclass Cannot Override staticMethods in Its Superclass (continued) 20
  • 21. A Subclass Cannot Override finalMethods in Its Superclass 21 Subclass cannot override methods declared final in superclass final modifier Does not allow method to be overridden Virtual method calls Default in Java Method used is determined when program runs Type of object used might not be known until method executes
  • 22. A Subclass Cannot Override finalMethods in Its Superclass (continued) 22 Advantage to making method final Compiler knows there is only one version of method Compiler knows which method version will be used Can optimize program’s performance By removing calls to final methods Replacing them with expanded code of their definitions At each method call location Called inlining
  • 23. A Subclass Cannot Override Methodsin a final Superclass 23 Declare class final All of its methods are final Regardless of which access modifier precedes method name Cannot be a parent class
  • 24. A Subclass Cannot Override Methodsin a final Superclass (continued) 24
  • 25. You Do It 25 Creating a superclass and an application to use it Creating a subclass and an application to use it Creating a subclass method that overrides a superclass method Understanding the role of constructors in inheritance
  • 26. You Do It (continued) 26 Inheritance when the superclass requires constructor arguments Accessing an overridden superclass method from within a subclass
  • 27. Don’t Do It 27 Don’t capitalize the “o” in the instanceof operator Don’t try to directly access private superclass members from a subclass Don’t forget to call a superclass constructor from within a subclass constructor if the superclass does not contain a default constructor
  • 28. Summary 28 Inheritance Mechanism that enables one class to inherit both behavior and attributes of another class Keyword extends Achieve inheritance in Java Polymorphism Act of using same method name to indicate different implementations
  • 29. Summary (continued) 29 Use a superclass method within a subclass Use keyword super to access it Information hiding Concept of keeping data private Keyword protected Intermediate level of security between public and private access Subclass cannot override methods Declared static in superclass Declared final or class final
  • 30. 30 Quick Quiz 3 The concept of keeping data private is known as ____. 2. Using the keyword ____ provides you with an intermediate level of security between public and private access. 3. True or False: Although a child class cannot inherit its parent’s static methods, it can access its parent’s static methods the same way any other class can. 4. You can declare a class to be ____. When you do, all of its methods are final, regardless of which access modifier precedes the method name.