SlideShare a Scribd company logo
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.

More Related Content

What's hot

Chapter 5 declaring classes & oop
Chapter 5 declaring classes  & oopChapter 5 declaring classes  & oop
Chapter 5 declaring classes & oop
sshhzap
 
C#
C#C#
Hemajava
HemajavaHemajava
Hemajava
SangeethaSasi1
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
homeworkping9
 
Mixin it up
Mixin it upMixin it up
Mixin it up
Nikhil Shahi
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access Specifier
DeeptiJava
 
Mca 2nd sem u-3 inheritance
Mca 2nd  sem u-3 inheritanceMca 2nd  sem u-3 inheritance
Mca 2nd sem u-3 inheritance
Rai University
 
14 interface
14  interface14  interface
14 interface
Ravindra Rathore
 
Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13
Vince Vo
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
HrithikShinde
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
Gaurav Mehta
 
Simple java program
Simple java programSimple java program
Simple java program
shalinikarunakaran1
 
Types of methods in python
Types of methods in pythonTypes of methods in python
Types of methods in python
Aravindreddy Mokireddy
 
Bca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritanceBca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritance
Rai 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 answers
w3asp dotnet
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
Abid Kohistani
 
Chap02
Chap02Chap02
Chap02
Jotham Gadot
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
Muthukumaran Subramanian
 
Java interface
Java interfaceJava interface
Java interface
GaneshKumarKanthiah
 
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
Harshal Misalkar
 

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

Similar to Chapter 05 polymorphism extra

Inheritance in java
Inheritance in javaInheritance in java
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
Victer Paul
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
Rosie Jane Enomar
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
Sourabrata Mukherjee
 
Chapter 3i
Chapter 3iChapter 3i
Chapter 3i
siragezeynu
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
M. Raihan
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
Thakur Amit Tomer
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
raksharao
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Kumar
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
SmrutiShah9
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
Terry Yoast
 
javainheritance
javainheritancejavainheritance
javainheritance
Arjun Shanka
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
HarshithaAllu
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
Deepak Singh
 
Java presentation
Java presentationJava presentation
Java presentation
Akteruzzaman .
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
sotlsoc
 
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 Qestions
Arun 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 programming
WondimuBantihun1
 

Similar to 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
 

Recently uploaded

Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
Edge AI and Vision Alliance
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Neo4j
 

Recently uploaded (20)

Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
 

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.