advertisement

Polymorphism

50 %
50 %
advertisement
Information about Polymorphism
Entertainment

Published on December 31, 2007

Author: Carlton

Source: authorstream.com

advertisement

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

Add a comment

Related presentations

Related pages

Polymorphismus – Wikipedia

Ursprünglich stand die Bezeichnung Polymorphismus für einen Begriff, der nahezu ausschließlich äußerlich sichtbare Merkmale erfasste, also das ...
Read more

Polymorphism (biology) - Wikipedia, the free encyclopedia

Polymorphism in biology and zoology is the occurrence of two or more clearly different morphs or forms, also referred to as alternative phenotypes, in the ...
Read more

Polymorphism - Wikipedia, the free encyclopedia

Polymorphism, polymorphic or polymorph, from the Greek roots "poly" (many) and "morphe" (form, shape, structure), may refer to:
Read more

Polymorphismus - DocCheck Flexikon

von griechisch: poly- - viel; morphe - Gestalt Englisch: polymorphism. Definition. Als Polymorphismus bezeichnet man in der Biologie bzw. Genetik das ...
Read more

Einzelnukleotid-Polymorphismus – Wikipedia

Mit dem Begriff Einzelnukleotid-Polymorphismus (SNP, engl. Single Nucleotide Polymorphism; im Laborjargon gesprochen: ‚Snip‘) wird eine Variation eines ...
Read more

Polymorphism (C# Programming Guide)

Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance. Polymorphism is a Greek word ...
Read more

dict.cc | polymorphism | Wörterbuch Englisch-Deutsch

Übersetzung für polymorphism im Englisch-Deutsch-Wörterbuch dict.cc.
Read more

Polymorphism (The Java™ Tutorials > Learning the Java ...

The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages.
Read more

polymorphism | biology | Britannica.com

Polymorphism, in biology, a discontinuous genetic variation resulting in the occurrence of several different forms or types of individuals among the ...
Read more

Polymorphismus – mehr als nur Mutation

Polymorphismus ist ein Fachausdruck aus der Biologie und bedeutet Vielgestaltigkeit. Früher wurde dieser Begriff hauptsächlich zur Beschreibung der ...
Read more