SlideShare uma empresa Scribd logo
1 de 41
Chapter 13 Inheritance and Polymorphism
Chapter 13 Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Defining Classes with Inheritance ,[object Object],[object Object],[object Object],[object Object]
Modeling Two Types of Students ,[object Object],[object Object],[object Object],[object Object]
Classes for the Class Roster ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Inheritance Hierarchy
The Protected Modifier ,[object Object],[object Object],[object Object]
Polymorphism ,[object Object],[object Object],Pet myPet; myPet =  new  Dog () ; . . . myPet =  new  Cat () ;
Creating the roster Array ,[object Object],Student roster =  new  Student [ 40 ] ; . . . roster [ 0 ]  =  new  GraduateStudent () ; roster [ 1 ]  =  new  UndergraduateStudent () ; roster [ 2 ]  =  new  UndergraduateStudent () ; . . .
State of the roster Array ,[object Object]
Sample Polymorphic Message ,[object Object],[object Object],[object Object],for   ( int   i = 0; i < numberOfStudents; i++ ) { roster [ i ] .computeCourseGrade () ; }
The instanceof Operator ,[object Object],[object Object],int   undergradCount = 0; for   ( int  i = 0; i < numberOfStudents; i++ ) { if   (  roster [ i ]  instanceof   UndergraduateStudent  ) { undergradCount++; } }
Inheritance and Member Accessibility ,[object Object]
The Effect of Three Visibility Modifiers
Accessibility of Super from Sub ,[object Object]
Accessibility from Another Instance ,[object Object]
Inheritance and Constructors ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Abstract Superclasses and Abstract Methods ,[object Object],[object Object],[object Object]
Definition: Abstract Class ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Case 1 ,[object Object],[object Object],[object Object]
Case 2 ,[object Object],[object Object],[object Object],[object Object]
Which Approach to Use ,[object Object],[object Object]
Inheritance versus Interface ,[object Object],[object Object],[object Object],[object Object]
Problem Statement ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Overall Plan ,[object Object],[object Object],[object Object],[object Object],[object Object]
Development Steps ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Step 1 Design ,[object Object],[object Object]
Step 1 Code Directory:   Chapter13/Step1 Source Files:  ComputeGrades.java Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE.
Step 1 Test ,[object Object],[object Object]
Step 2 Design ,[object Object],[object Object],for   each element i in the roster array  { output the name of  roster [ i ] ; output the test scores of  roster [ i ] ; output the course grade of  roster [ i ] ; skip to the next line ; }
Step 2 Code Directory:   Chapter13/Step2 Source Files:  ComputeGrades.java
Step 2 Test ,[object Object],[object Object]
Step 3 Design ,[object Object],[object Object],[object Object],[object Object]
Step 3 Code Directory:   Chapter13/Step3 Source Files:  ComputeGrades.java
Step 3 Test ,[object Object],[object Object],[object Object]
Step 4 Design ,[object Object],[object Object],get the filename from the user ; if   ( the filename is provided ) read in data and build the roster array ; else output an error message ;
The buildRoster Method ,[object Object],set bufReader for input ; while  (  !done  ) { line  =  get next line ; if  ( line is END ) {   done =  true ; }  else  {   student = createStudent (  line  ) ;   if  ( student !=  null ) { roster [ studentCount ]  = student;  //add to roster studentCount++;   }   }
The createStudent Method ,[object Object],StringTokenizer parser =  new   StringTokenizer (  line  ) ; String type; try   { type = parser.nextToken () ; if   ( type.equals ( UNDER_GRAD )  || type.equals ( GRAD )) { student = newStudentWithData ( type, parser ) ; }  else   {  //invalid type is encountered student =  null ; } }  catch   ( NoSuchElementException e ) {  //no token student =  null ; } return   student;
Step 4 Code Directory:   Chapter13/Step4 Source Files:  ComputeGrades.java
Step 4 Test ,[object Object]
Step 5: Finalize and Improve ,[object Object],[object Object],[object Object],[object Object],[object Object]

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
C++ radna skripta
C++ radna skriptaC++ radna skripta
C++ radna skripta
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Advanced Interfaces and Repositories in Laravel
Advanced Interfaces and Repositories in LaravelAdvanced Interfaces and Repositories in Laravel
Advanced Interfaces and Repositories in Laravel
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2
 
Java Annotations
Java AnnotationsJava Annotations
Java Annotations
 
Files in c++
Files in c++Files in c++
Files in c++
 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
 
Java Exception Handling and Applets
Java Exception Handling and AppletsJava Exception Handling and Applets
Java Exception Handling and Applets
 
WEKA:The Command Line Interface
WEKA:The Command Line InterfaceWEKA:The Command Line Interface
WEKA:The Command Line Interface
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Python-Encapsulation.pptx
Python-Encapsulation.pptxPython-Encapsulation.pptx
Python-Encapsulation.pptx
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
Exception handling
Exception handling Exception handling
Exception handling
 
Java collections
Java collectionsJava collections
Java collections
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 

Destaque

Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
Deepak Singh
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
Neveen Reda
 

Destaque (20)

CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
Java programming lab assignments
Java programming lab assignments Java programming lab assignments
Java programming lab assignments
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]
 
Apclass
ApclassApclass
Apclass
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
Loop c++
Loop c++Loop c++
Loop c++
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
Array in c++
Array in c++Array in c++
Array in c++
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
Structure in c
Structure in cStructure in c
Structure in c
 
Structure in c
Structure in cStructure in c
Structure in c
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 

Semelhante a Chapter 13 - Inheritance and Polymorphism

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
Maryo Manjaruni
 
Application package
Application packageApplication package
Application package
JAYAARC
 
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdfJAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
nofakeNews
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
Connex
 

Semelhante a Chapter 13 - Inheritance and Polymorphism (20)

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
 
Classes2
Classes2Classes2
Classes2
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
 
Application package
Application packageApplication package
Application package
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
 
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdfJAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
 
Object Oriented Principles
Object Oriented PrinciplesObject Oriented Principles
Object Oriented Principles
 
Unit 1 Java
Unit 1 JavaUnit 1 Java
Unit 1 Java
 
Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
Python_Unit_3.pdf
Python_Unit_3.pdfPython_Unit_3.pdf
Python_Unit_3.pdf
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
Essential language features
Essential language featuresEssential language features
Essential language features
 
UNIT1-JAVA.pptx
UNIT1-JAVA.pptxUNIT1-JAVA.pptx
UNIT1-JAVA.pptx
 
12th ip CBSE chapter 4 oop in java notes complete
12th ip CBSE  chapter 4 oop in java notes complete12th ip CBSE  chapter 4 oop in java notes complete
12th ip CBSE chapter 4 oop in java notes complete
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 

Mais de Eduardo Bergavera (9)

CLP Session 5 - The Christian Family
CLP Session 5 - The Christian FamilyCLP Session 5 - The Christian Family
CLP Session 5 - The Christian Family
 
What is Python?
What is Python?What is Python?
What is Python?
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
 
Chapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software DevelopmentChapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software Development
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

Chapter 13 - Inheritance and Polymorphism

  • 1. Chapter 13 Inheritance and Polymorphism
  • 2.
  • 3.
  • 4.
  • 5.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14. The Effect of Three Visibility Modifiers
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28. Step 1 Code Directory: Chapter13/Step1 Source Files: ComputeGrades.java Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE.
  • 29.
  • 30.
  • 31. Step 2 Code Directory: Chapter13/Step2 Source Files: ComputeGrades.java
  • 32.
  • 33.
  • 34. Step 3 Code Directory: Chapter13/Step3 Source Files: ComputeGrades.java
  • 35.
  • 36.
  • 37.
  • 38.
  • 39. Step 4 Code Directory: Chapter13/Step4 Source Files: ComputeGrades.java
  • 40.
  • 41.

Notas do Editor

  1. A variable of class X may not refer to an object from the superclass or sibling classes of X. Sibling classes are those that share the common ancestor class. For example, the following statements are invalid: Dog myDog = new Cat () ; Cat myCat = new Pet () ;
  2. Please use your Java IDE to view the source files and run the program.