logging in or signing up Polymorphism Carlton Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 1226 Category: Entertainment License: All Rights Reserved Like it (5) Dislike it (0) Added: December 31, 2007 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... By: damiannn10 (12 month(s) ago) Ineed download this shit plis Saving..... Post Reply Close Saving..... Edit Comment Close By: hassan_umt2011 (13 month(s) ago) i need download plz Saving..... Post Reply Close Saving..... Edit Comment Close By: waheed (16 month(s) ago) i need to download please . Saving..... Post Reply Close Saving..... Edit Comment Close By: prasad1 (46 month(s) ago) good Saving..... Post Reply Close Saving..... Edit Comment Close Premium member Presentation Transcript Polymorphism&Interfaces: Polymorphism & Interfaces Dancing Robots & SortingInheritance Hierarchy: Inheritance Hierarchy Overriding the Move Method: Overriding the Move MethodArrays 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 valuesA 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 referencesDifferent 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 objectsCasting: 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 instanceOfParameters & 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 objectThe 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 InterfaceInterfaces 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 InterfaceImplementing 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 You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
Polymorphism Carlton Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 1226 Category: Entertainment License: All Rights Reserved Like it (5) Dislike it (0) Added: December 31, 2007 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... By: damiannn10 (12 month(s) ago) Ineed download this shit plis Saving..... Post Reply Close Saving..... Edit Comment Close By: hassan_umt2011 (13 month(s) ago) i need download plz Saving..... Post Reply Close Saving..... Edit Comment Close By: waheed (16 month(s) ago) i need to download please . Saving..... Post Reply Close Saving..... Edit Comment Close By: prasad1 (46 month(s) ago) good Saving..... Post Reply Close Saving..... Edit Comment Close Premium member Presentation Transcript Polymorphism&Interfaces: Polymorphism & Interfaces Dancing Robots & SortingInheritance Hierarchy: Inheritance Hierarchy Overriding the Move Method: Overriding the Move MethodArrays 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 valuesA 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 referencesDifferent 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 objectsCasting: 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 instanceOfParameters & 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 objectThe 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 InterfaceInterfaces 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 InterfaceImplementing 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