SlideShare une entreprise Scribd logo
1  sur  98
Introduction to Computer Science II COSC 1320/6305 Lecture 6:  Polymorphism and Abstract Classes (Chapter 8)
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Class Participation NetBeans Projects 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Sales Records ,[object Object],[object Object],[object Object],[object Object],[object Object],7-
7- CLASS Participation  A ! (pp. 479)
 
Let’s create two  objects ,[object Object],[object Object],[object Object],8-
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into the  constructor !
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into the  constructor !
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Which  object ’s  method  is this going to Execute?   discount . lessThan ! ( lessThan  is   inherited   from   Sale !!! )
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into  lessThan !
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into  this   bill() !
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into  Sale   bill() !
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Returned!
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Introduction to  Polymorphism ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8-
Introduction to  Polymorphism ,[object Object],[object Object],[object Object],8-
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Late Binding ,[object Object],[object Object],[object Object],8-
Late Binding ,[object Object],[object Object],[object Object],8-
The  Sale   Class ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8-
The  Sale   Class ,[object Object],[object Object],[object Object],8-
The  DiscountSale   Class ,[object Object],[object Object],[object Object],8-
The  Sale   Class ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8- It knows that it is a  Discount   object !
The  Sale  and  DiscountSale   Classes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8-
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],The  Sale  and  DiscountSale   Class es 8-
The  Sale  and  DiscountSale   Classes ,[object Object],[object Object],[object Object],[object Object],[object Object],8-
Let’s create two more  objects ,[object Object],[object Object],[object Object],8-
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Which  object ’s  method  is this going to Execute?   discount . lessThan ! ( lessThan  is   inherited   from   Sale !!! ) Which  object ’s  method  is this going to Execute?   specialPrice . equalDeals ! ( equalDeals  is   inherited   from   Sale !!! ) Step into  equalDeals !
7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. It knows to call the  bill method  of the  specialPrice !!!!
Pitfall:  No Late Binding  for  Static   Methods ,[object Object],[object Object],[object Object],[object Object],[object Object]
7- CLASS Participation  B ! (pp. 488)
Let’s call  static  announcement()  with class Name ,[object Object],[object Object],8-
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Call  static  method announcement()  by using the  class  Sale  and  DiscountSale !!!! Step into  Sale   announcement() !
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into  DiscountSale   announcement() !
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Pitfall:  No Late Binding  for  Static  Methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8-
Let’s create two  objects ,[object Object],[object Object],8-
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into  s . announcement() !
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into  discount . announcement() !
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Let’s create one  object ,[object Object],[object Object],[object Object],8-
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. It knows that  discount2  is going to be a  Sale   object !!!!
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Which  announcement()   will be invoked???
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Pitfall:  No Late Binding  for  Static  Methods ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Pitfall:  No Late Binding  for  Static  Methods
Pitfall:  No Late Binding  for  Static  Methods ,[object Object],[object Object],[object Object],8-
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
The  final  Modifier ,[object Object],[object Object],[object Object],[object Object],8-
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Late Binding  with  toString 8-
Late Binding  with  toString ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8-
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
An  object  knows the Definitions of  its Methods ,[object Object],[object Object],[object Object],[object Object],[object Object],8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
8-
Introduction to  abstract  Classes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
CLASS Participation  C ! (pp. 501)
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Introduction to  abstract  Classes ,[object Object],[object Object],[object Object],[object Object],8-
Introduction to  abstract  Classes ,[object Object],[object Object],[object Object],[object Object]
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Introduction to  abstract  Classes ,[object Object],[object Object],[object Object],[object Object]
Abstract  Method ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8-
Abstract  Class ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8-
Abstract  Class ,[object Object],[object Object],[object Object],8- Step into the  constructor !
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Pitfall:  You Cannot Create Instances of an  Abstract  Class ,[object Object],[object Object],[object Object],[object Object],8-
Tip:  An  Abstract  Class  Is a Type ,[object Object],[object Object],[object Object],8-
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Upcasting  and Downcasting ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8-
Upcasting and  Downcasting ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Pitfall:  Downcasting ,[object Object],[object Object],[object Object],8-
Tip:  Checking to See if  Downcasting  is Legitimate ,[object Object],[object Object],[object Object],[object Object],[object Object],8-
A First Look at the  clone   Method ,[object Object],[object Object],[object Object],[object Object],[object Object]
A First Look at the  clone   Method ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8-
A First Look at the  clone   Method ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8-
Pitfall:  Sometime the  clone   Method  Return Type is  Object ,[object Object],[object Object],[object Object],[object Object],8-
Pitfall:  Sometime the  clone   Method  Return Type is  Object ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8-
Pitfall:  Sometime the  clone   Method  Return Type is  Object ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],8-
Pitfall:  Limitations of  Copy Constructors ,[object Object],[object Object],[object Object],[object Object],8-
Pitfall:  Limitations of  Copy Constructors ,[object Object],[object Object],[object Object],[object Object],8-

Contenu connexe

Tendances (20)

class and objects
class and objectsclass and objects
class and objects
 
Lecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With PythonLecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With Python
 
L2
L2L2
L2
 
Decaf language specification
Decaf language specificationDecaf language specification
Decaf language specification
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introduction
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
 
01 sm3 xml_xp_02
01 sm3 xml_xp_0201 sm3 xml_xp_02
01 sm3 xml_xp_02
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operators
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Chapter 8.3
Chapter 8.3Chapter 8.3
Chapter 8.3
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
Java Generics
Java GenericsJava Generics
Java Generics
 

Similaire à Lecture 6 polymorphism

PorfolioReport
PorfolioReportPorfolioReport
PorfolioReportAlbert Chu
 
Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2Haitham Raik
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 
Refactoring legacy code: step-by-step examples
Refactoring legacy code: step-by-step examplesRefactoring legacy code: step-by-step examples
Refactoring legacy code: step-by-step examplesEndava
 
Sales force certification-lab
Sales force certification-labSales force certification-lab
Sales force certification-labAmit Sharma
 
BELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdf
BELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdfBELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdf
BELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdfaminaENT
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4dplunkett
 
Companies and people often buy and sell stocks. Often they buy the sa.pdf
Companies and people often buy and sell stocks. Often they buy the sa.pdfCompanies and people often buy and sell stocks. Often they buy the sa.pdf
Companies and people often buy and sell stocks. Often they buy the sa.pdfrydeberghal13313
 
Sales force certification-lab
Sales force certification-labSales force certification-lab
Sales force certification-labAmit Sharma
 
OOP-Advanced Programming with c++
OOP-Advanced Programming with c++OOP-Advanced Programming with c++
OOP-Advanced Programming with c++Mohamed Essam
 
Advanced PHPUnit Testing
Advanced PHPUnit TestingAdvanced PHPUnit Testing
Advanced PHPUnit TestingMike Lively
 
Overview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company indiaOverview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company indiaJignesh Aakoliya
 

Similaire à Lecture 6 polymorphism (20)

PorfolioReport
PorfolioReportPorfolioReport
PorfolioReport
 
Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Refactoring legacy code: step-by-step examples
Refactoring legacy code: step-by-step examplesRefactoring legacy code: step-by-step examples
Refactoring legacy code: step-by-step examples
 
Sales force certification-lab
Sales force certification-labSales force certification-lab
Sales force certification-lab
 
BELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdf
BELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdfBELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdf
BELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdf
 
Savitch ch 04
Savitch ch 04Savitch ch 04
Savitch ch 04
 
java tr.docx
java tr.docxjava tr.docx
java tr.docx
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
 
Companies and people often buy and sell stocks. Often they buy the sa.pdf
Companies and people often buy and sell stocks. Often they buy the sa.pdfCompanies and people often buy and sell stocks. Often they buy the sa.pdf
Companies and people often buy and sell stocks. Often they buy the sa.pdf
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Sales force certification-lab
Sales force certification-labSales force certification-lab
Sales force certification-lab
 
OOP-Advanced Programming with c++
OOP-Advanced Programming with c++OOP-Advanced Programming with c++
OOP-Advanced Programming with c++
 
Advanced PHPUnit Testing
Advanced PHPUnit TestingAdvanced PHPUnit Testing
Advanced PHPUnit Testing
 
Overview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company indiaOverview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company india
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Lecture 6 polymorphism

  • 1. Introduction to Computer Science II COSC 1320/6305 Lecture 6: Polymorphism and Abstract Classes (Chapter 8)
  • 2. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 3. Class Participation NetBeans Projects 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 4.
  • 5. 7- CLASS Participation A ! (pp. 479)
  • 6.  
  • 7.
  • 8. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into the constructor !
  • 9. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 10. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into the constructor !
  • 11. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 12. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 13. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Which object ’s method is this going to Execute? discount . lessThan ! ( lessThan is inherited from Sale !!! )
  • 14. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into lessThan !
  • 15. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 16. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into this bill() !
  • 17. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 18. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into Sale bill() !
  • 19. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 20. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Returned!
  • 21. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 22.
  • 23.
  • 24. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Which object ’s method is this going to Execute? discount . lessThan ! ( lessThan is inherited from Sale !!! ) Which object ’s method is this going to Execute? specialPrice . equalDeals ! ( equalDeals is inherited from Sale !!! ) Step into equalDeals !
  • 36. 7- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. It knows to call the bill method of the specialPrice !!!!
  • 37.
  • 38. 7- CLASS Participation B ! (pp. 488)
  • 39.
  • 40. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Call static method announcement() by using the class Sale and DiscountSale !!!! Step into Sale announcement() !
  • 41. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 42. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into DiscountSale announcement() !
  • 43. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 44. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 45.
  • 46.
  • 47. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into s . announcement() !
  • 48. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 49. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Step into discount . announcement() !
  • 50. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 51.
  • 52. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. It knows that discount2 is going to be a Sale object !!!!
  • 53. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 54. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Which announcement() will be invoked???
  • 55. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 56. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 57. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 58.
  • 59.
  • 60.
  • 61. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 62.
  • 63.
  • 64.
  • 65. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 66.
  • 67. 8-
  • 68.
  • 69. CLASS Participation C ! (pp. 501)
  • 70. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 71.  
  • 72. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 73.
  • 74.
  • 75. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 81.
  • 82.
  • 83. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 84. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 85. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 86. 8- Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.