Polymorphism

Information about Polymorphism

Published on December 31, 2007

Author: Carlton

Source: authorstream.com

Content

Polymorphism & Interfaces:  Polymorphism & Interfaces Dancing Robots & Sorting Inheritance Hierarchy:  Inheritance Hierarchy Overriding the Move Method:  Overriding the Move Method Arrays of Robots:  Arrays of Robots Arrays in Java:  Arrays in Java Two step process to use an array declare the array and the size of the array fill the array with values initially an array of objects is filled with null references In Java, subscripts for arrays always start at 0 2-D arrays are actually arrays of arrays Vector a class in the java.util library acts like a partially-filled array with automatic resizing need to cast when retrieving values A Chorus Line of LeftDancers:  A Chorus Line of LeftDancers A Chorus Line of LeftDancers:  A Chorus Line of LeftDancers Predict! How will the robots in the chorus line move? Remember, the method public int move(int numSteps) is found in the RobotSE class. A Chorus Line of Polymorphism:  A Chorus Line of Polymorphism We would like to have Robot, LeftDancer, and RightDancer objects in the same array We can do this Declare the array as type Robot fill the array with objects from the Robot class or any class the inherits (directly or indirectly) from the Robot class Robot[] chorusLine = new Robot[SIZE]; Robot[i] = new LeftDancer(…); The variable type determines the valid method calls in the code The constructor determines the actual capabilities of the object OBJECTS ALWAYS KNOW WHAT THEY ARE! Different Kinds of Robots:  Different Kinds of Robots Declaring the Array:  Declaring the Array chorusLine[0] = new LeftDancer(…); chorusLine[1] = new Robot(…); chorusLine[2] = new RightDancer(…); Robot[] chorusLine; chorusLine = new Robot[3]; Space for the information stored in a LeftDancer object Enough space forthree references Different Kinds of Robots:  Different Kinds of Robots Predict! What will happen when you run the program? How do we make the “dancing robots” pirouette?:  How do we make the “dancing robots” pirouette? We need to remind these particular robots that they have the ability to pirouette cast them back to LeftDancer or RightDancer How do we know which members of the chorus line can piroutte? use instanceOf to identify the LeftDancer and the RightDancer objects Casting:  Casting You can convert variables to a different type by casting assuming it is a valid conversion Casting primitives examples intVar = (int)doubleVar; charVar = (char)intVar; //(ascii code) Casting objects You can cast an object to a type equal to its own class or any class from which the object inherits Casting Robots:  Casting Robots Robot robot1 = new Robot(…); Robot robot2 = new LeftDancer(…); LeftDancer robot3; RightDancer robot4; Will these lines of code run? robot4 = robot2; robot4 = (RightDancer)robot2; robot3 = (LeftDancer)robot1; robot1 = robot2; robot3 = (LeftDancer)robot1; Casting Using instanceOf:  Casting Using instanceOf Parameters & Polymorphism:  Parameters & Polymorphism parameters are a natural place to use polymorphism for example, a method signature which includes a Robot parameter, will accept RobotSE, LeftDancer, and RightDancer objects in the method calls A couple of notes about parameters in Java all parameters are passed by value (copy the value from the actual parameter into the formal parameter) any Object parameters are references to the object The Problem with Sorting:  The Problem with Sorting We would like to write program that will perform the bubble sort on some data. What if we don’t know what kind of data we are going to sort? What if we want to sort some integers and then sort some words? Do we really need to write a different method for each set of data? The Bubblesort Algorithm:  The Bubblesort Algorithm for i = 1 .. numItems for j = 1 .. numItems if (value at j) < (value at j) swap values at i & j If we could only generalize any collection that we wanted to sort, then we would only have to write one bubblesort method. We can; we need an Interface. The Sortable Interface:  The Sortable Interface Interfaces in Java:  Interfaces in Java An interface lists method signatures and comments only. NO constructors in an interface. NO instance variables in an interface. Only usable if there is at least one class which implements the interface. Many classes may implement the same interface. (Polymorphism) Can be used as a variable type. Polymorphism with an Interface:  Polymorphism with an Interface Implementing an Interface:  Implementing an Interface The class declaration is public class MyClass implements MyInterface The class must implement ALL of the methods listed in the interface. The class may include more methods. (Once class can implement more than one interface.) Implementing the Sortable Interface:  Implementing the Sortable Interface Decide what kind of collection you want to sort an array of int (easiest) an array of Strings a Vector of Integers Implement the interface creating a class with the appropriate instance variables, constructor, and code to fill the methods from Sortable. Construct an object from this class, and send it to the bubblesort algorithm to be sorted. Polymorphism with an Interface:  Polymorphism with an Interface

Related presentations


Other presentations created by Carlton

Consumer
19. 11. 2007
0 views

Consumer

first06b
02. 10. 2007
0 views

first06b

Nyberg yeast
08. 11. 2007
0 views

Nyberg yeast

udd
10. 12. 2007
0 views

udd

Lab Setup
05. 11. 2007
0 views

Lab Setup

3 MOVE
06. 11. 2007
0 views

3 MOVE

drire
15. 11. 2007
0 views

drire

KRISTAL2006 04
20. 11. 2007
0 views

KRISTAL2006 04

DHCREALV2
22. 11. 2007
0 views

DHCREALV2

modern slow dance
23. 11. 2007
0 views

modern slow dance

Ethnic Geography Part II
23. 11. 2007
0 views

Ethnic Geography Part II

P Naming Elephants Presentation
26. 11. 2007
0 views

P Naming Elephants Presentation

ancientrome
31. 10. 2007
0 views

ancientrome

FarmtoBusiness
27. 12. 2007
0 views

FarmtoBusiness

pruning avon landscape school
03. 01. 2008
0 views

pruning avon landscape school

Clinical
04. 01. 2008
0 views

Clinical

5 Unit 12
04. 01. 2008
0 views

5 Unit 12

WhittakerPaper
07. 01. 2008
0 views

WhittakerPaper

CEROS TechEnterprise 2005
07. 01. 2008
0 views

CEROS TechEnterprise 2005

ISU Trondheim 2006
07. 12. 2007
0 views

ISU Trondheim 2006

Parks Decsion Tree
28. 11. 2007
0 views

Parks Decsion Tree

deussen
02. 01. 2008
0 views

deussen

Onion handout only2006
07. 11. 2007
0 views

Onion handout only2006

SalahuddinAminuzzaman
25. 12. 2007
0 views

SalahuddinAminuzzaman

1 2006 parasitology definitions
24. 10. 2007
0 views

1 2006 parasitology definitions

mktrec
24. 02. 2008
0 views

mktrec

voyager tutorial
27. 02. 2008
0 views

voyager tutorial

MP10SpecialRelativit y2
06. 11. 2007
0 views

MP10SpecialRelativit y2

Maher Creativity 2007 08 14
28. 11. 2007
0 views

Maher Creativity 2007 08 14

Lifeline v4c
31. 10. 2007
0 views

Lifeline v4c

Travel Health
27. 03. 2008
0 views

Travel Health

Musical 1
02. 11. 2007
0 views

Musical 1

Outline2
13. 11. 2007
0 views

Outline2

wac2004ronblum
13. 04. 2008
0 views

wac2004ronblum

Agrofuels Driving Climate Change
26. 11. 2007
0 views

Agrofuels Driving Climate Change

2007 05 09bbh download
26. 10. 2007
0 views

2007 05 09bbh download

Bartenders Karen Palmersheim
11. 12. 2007
0 views

Bartenders Karen Palmersheim

Dynamic Licensing
06. 11. 2007
0 views

Dynamic Licensing

jose nov 04
30. 10. 2007
0 views

jose nov 04

1183379282 IIIA N S results
26. 10. 2007
0 views

1183379282 IIIA N S results

CLS Euro pres
02. 01. 2008
0 views

CLS Euro pres

cours5
01. 11. 2007
0 views

cours5

Homestake DOSSECC 6 2006b
03. 12. 2007
0 views

Homestake DOSSECC 6 2006b

ritt
29. 10. 2007
0 views

ritt

true fri pps2
28. 12. 2007
0 views

true fri pps2

27 Piro ops
30. 10. 2007
0 views

27 Piro ops

KUS5 trebilcock
01. 12. 2007
0 views

KUS5 trebilcock

Subsea experience 1
07. 11. 2007
0 views

Subsea experience 1

pred desvios
28. 12. 2007
0 views

pred desvios

Eric Van Heck
12. 12. 2007
0 views

Eric Van Heck