Polymorphism Simple but profound!.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Basics of Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 © Mitchell Wand, This work is licensed under a Creative Commons.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
Chapter 3 Extending the Robot Programming Language.
1 Ch. 3 Ch.3 Classes & Stepwise Refinement STEP 1 Define a new class of robot (see next slide) When designing a new class (whether that’s robots, cars,
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
1 Inheritance in Java Behind the scenes: new Objects from old.
Polymorphism Are there different ways to solve the Harvester problem? – Robot teams – instead of one robot to solve a problem, let’s get a team of robots.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Extending the Robot Programming Language In the Robot world 1 mile = 8 blocks Suppose we want a robot to run a marathon (26+ miles)? Does our program have.
Chapter 5 Conditionally Executing Instructions
1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 3.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Karel J. Robot Tool for learning OOP (Lecture covers Ch. 1 and 2)
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 Note: Original slides provided by and modified for Mr. Heath’s AP Computer Science A classwww.apComputerScience.com.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 4.
1 Ch Object References a.k.a. variables Teams of Robots (e.g.) –Could have 1 robot harvest 6 rows (we’ve seen that) –Could have 3 robots each.
1 Note: Original slides provided by and modified for Mr. Smith ’ s AP Computer Science A classwww.apComputerScience.com.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
1 Ch. 3 Ch.3 Classes & Stepwise Refinement STEP 1 Define a new class of robot (see next slide) When designing a new class (whether that’s robots, cars,
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Chapter 3 Extending the Robot Programming Language.
Karel – Primitive Instructions
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
Ch.3 Classes & Stepwise Refinement
Ch.3 Classes STEP 1 Define a new class of robot (see next slide)
Karel – Primitive Instructions
Karel J Robot Chapter 4 B.
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object Oriented Programming
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
More inheritance, Abstract Classes and Interfaces
Interface.
Extending Classes.
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
A Gentle Introduction to the Art of Object Oriented Programming
Ch.3 Classes & Stepwise Refinement
Java – Inheritance.
Java Inheritance.
Objects First with Java A Practical Introduction using BlueJ
Object References a.k.a. variables
Workshop for Programming And Systems Management Teachers
Ch.3 Classes & Stepwise Refinement
Chapter 14 Abstract Classes and Interfaces
Ch.3 Classes & Stepwise Refinement
Objects First with Java A Practical Introduction using BlueJ
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Karel – Primitive Instructions
Presentation transcript:

Polymorphism Simple but profound!

Homonym A word that sounds and spells the same as another. Ex: bear can be a verb (carry a burden) or a noun( a large, hairy animal). You determine the correct word from context clues.

Polymorphism •Powerful example: You are all objects - if I tell all of you to “takeABreak()”, you all will hear the same message but will act in different ways (some of you will sleep, some will walk out the door and eat something, some will try to leave school!, some will do work, etc.) - that’s polymorphism Sending the same message to different objects yet each individual object has a particular way to interpret (implement) the message

Polymorphism Simply Stated An object (robot) does what it understands to do when it gets a message. A method with the same spelling as another but with different behaviors.

Polymorphism- Overridden Methods Ex: let’s have 3 different types of bots MileWalker when move() is invoked, moves 1 mile DropBeeperAndWalker when move() is invoked, always drops a beeper and then moves one block forward BackwardWalker (sort of the Michael Jackson of robots!) when move() is invoked, moves one block backward for each of these new classes, we will only have to write one method, move() - each, however, will be implemented differently, and, in addition, override the original definition of move() inherited from UrRobot

References- objects from different classes to complete a task. Robot teams in programs Examples: 1) Each Letter in APCS is written by a different class of robot. 2) Harvester Could have 1 robot harvest 6 rows (we’ve seen that) Could have 3 robots each harvest 2 rows like this: public static void main() { Harvester botA = new Harvester(2,2,…,…); Harvester botB = new Harvester(4,2,…,…); Harvester botC = new Harvester(6,2,…,…); botA.move(); botA.harvestTwoRows(); botA.turnOff(); botB.move(); botB.harvestTwoRows(); botB.turnOff(); botC.move(); botC.harvestTwoRows(); botC.turnOff(); }

We can reorder the lines public static void main() { Harvester botA = new Harvester(2,2,…,…); botA.move(); botA.harvestTwoRows(); botA.turnOff(); Harvester botB = new Harvester(4,2,…,…); botB.move(); botB.harvestTwoRows(); botB.turnOff(); Harvester botC = new Harvester(6,2,…,…); botC.move(); botC.harvestTwoRows(); botC.turnOff(); }

Object References A reference reuses an existing name. George Foreman’s kids all named George Dude or Jon to refer to a person If each robot does it’s work before the next robot begins, we don’t need 3 different robot names to address the robots. Have one robot name declared and use it for all robots. Use assignment to change the object that a reference points to.

Use assignment to assign a specific object to a reference public static void main() { Harvester bot=null; //bot points to no robot bot = new Harvester(2,2,…,…); //bot points to a robot by assignment bot.move(); bot.harvestTwoRows(); botA.turnOff(); bot = new Harvester(4,2,…,…); //bot points to a new robot bot.harvestTwoRows(); botB.turnOff(); bot = new Harvester(6,2,…,…); //bot points to a new robot bot.harvestTwoRows(); botC.turnOff(); }

Garbage Collecting When a reference no longer points to an object, it is out of your program’s control. Messages can no longer be sent to it. The robot factory will know this is the case and collect the un-useable robot by it’s garbage collector system. Harvester bot=null; //bot points to no robot bot = new Harvester(2,2,…,…); //bot points to a robot by assignment … bot = new Harvester(4,2,…,…); //bot points to a new robot. Old reference severed. //Garbage collector picks up old robot at end of street 2 bot = new Harvester(6,2,…,…); //bot points to a new robot. Old reference severed. // Garbage collector picks up old robot at end of street 4

Object Reference- A Common Error What’s wrong here? Harvester bob = null; bob.harvestTwoRows(); NullPointerException an error in Java is called an exception

Abstract classes Sometimes we want to do several tasks, but the tasks are very similar. How can we build the classes to take advantage of the common parts of the task and yet distinguish the specific differences? Another way to say that is, how can we design the inheritance tree so that we don’t duplicate common code used among sub-classes, yet allow sub-classes to have some specific differences? The answer = use an abstract class…

Simple task to demo the need for an abstract class Here is a task for a team of robots. We want to lay down beepers in a 5-by-4 field. The odd-numbered rows have 2 beepers per corner, the even have 3. Here is how we’d organize that with what we currently know: UrRobot TwoRowLayer ThreeRowLayer layBeepers() putBeepers() Ch. 4.1 - 4.3

Create a class with the common methods- BeeperLayer On the previous slide, we saw that layBeepers() would have the exact same code for both types of beeper layer robots - namely: public class BeeperLayer extends UrRobot { //constructor goes here public void layBeepers() {move(); putBeepers(); move(); putBeepers(); move(); putBeepers(); move(); putBeepers(); move(); turnOff(); } public abstract void putBeepers(); //code to be provided later }

The two classes for Manufactured Robots for the Task public class TwoRowLayer extends BeeperLayer { // constructor goes here public void putBeepers() { putBeeper(); putBeeper(); } } public class ThreeRowLayer extends BeeperLayer { putBeeper(); putBeeper(); putBeeper(); }

Inheritance Hierarchy UrRobot BeeperLayer public void layBeepers() { … } public abstract void putBeepers(); TwoRowLayer public void putBeepers() { … } ThreeRowLayer public void putBeepers() { … } Ch. 4.1 - 4.3

Each robot interprets each method it gets as defined by it’s class. public static void main() { BeeperLayer lisa = null; lisa = new TwoRowLayer(1, 3 ,East, infinity); lisa.layBeepers(); lisa = new ThreeRowLayer(2, 3, East, infinity); lisa.layBeepers(); lisa = new TwoRowLayer(3, 3, East, infinity); lisa.layBeepers(); lisa = new ThreeRowLayer(4, 3, East, infinity); lisa.layBeepers(); lisa = new TwoRowLayer(5, 3, East, infinity); lisa.layBeepers(); }

Terminology & Concepts Using the BeeperLayer problem, pick one of these terms and demonstrate that you know what it means Polymorphism, reference, garbage collection, abstract class, abstract method