Object References a.k.a. variables

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 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,
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
1 Inheritance in Java Behind the scenes: new Objects from old.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
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.
Lecture 7 Polymorphism. Review: Constructors ●Parenthesis after constructor, not in class declaration ●The constructor makes the SamBot – do not “ new.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 3.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Karel J. Robot Tool for learning OOP (Lecture covers Ch. 1 and 2)
1 Note: Original slides provided by and modified for Mr. Heath’s AP Computer Science A classwww.apComputerScience.com.
Salman Marvasti Sharif University of Technology Winter 2015.
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.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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,
Overview of C++ Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOP Basics Classes & Methods (c) IDMS/SQL News
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Karel – Primitive Instructions
Inheritance ITI1121 Nour El Kadri.
Inheritance and Polymorphism
Ch.3 Classes & Stepwise Refinement
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Ch.3 Classes STEP 1 Define a new class of robot (see next slide)
Karel – Primitive Instructions
Karel J Robot Chapter 4 B.
Polymorphism Simple but profound!.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
CS2102: Lecture on Abstract Classes and Inheritance
Lesson 2: Building Blocks of Programming
More inheritance, Abstract Classes and Interfaces
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Ch.3 Classes & Stepwise Refinement
Java – Inheritance.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Java Inheritance.
CMSC 202 Generics.
Overview of C++ Polymorphism
Ch.3 Classes & Stepwise Refinement
Chapter 14 Abstract Classes and Interfaces
Ch.3 Classes & Stepwise Refinement
Interfaces.
Java Programming Language
Review of Previous Lesson
Review of Previous Lesson
Exceptions 10-May-19.
Classes and Methods 15-Aug-19.
Karel – Primitive Instructions
Presentation transcript:

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 harvest 2 rows like this: Harvester botA = new Harvester(2,2,…,…); Harvester botB = new Harvester(4,2,…,…); Harvester botC = new Harvester(6,2,…,…); botA.move(); botA.harvestTwoRows(); botB.move(); botB.harvestTwoRows(); botC.move(); botC.harvestTwoRows(); Ch. 4.1 - 4.3

Object References Could also intersperse the operations like this: // same instantiations (making the objects) botA.move(); botB.move(); botC.move(); botA.harvestTwoRows(); botB.harvestTwoRows(); botC.harvestTwoRows(); Ch. 4.1 - 4.3

Object References Could just use one reference like this: Harvester bot; bot = new Harvester(2,2,…,…); bot.move(); bot.harvestTwoRows(); bot = new Harvester(4,2,…,…); bot = new Harvester(6,2,…,…); a reference instantiating 3 objects we use assignment to assign a specific object to a reference Ch. 4.1 - 4.3

Object References - Common Error Harvester bob; bob.harvestTwoRows(); What’s wrong with the above? NullPointerException for now, an error in Java is called an exception do you think this error happens at run-time or compile-time? why? Binky Pointer Video Ch. 4.1 - 4.3

Object References References model what’s going on in the real world as well There are lots of “n00b” references - but the particular object (person) one is referring to depends on context and whom one is, in particular, referring to at the moment Well, these references are all neat and everything, but so what? Well, hold on a few more slides and you’ll see the power of using them - we’re headed toward an extremely important OO concept called Polymorphism. Ch. 4.1 - 4.3

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 - each individual object has a particular way to interpret (implement) the message so, back to code and a Java/Karel example… Ch. 4.1 - 4.3

Overriding move() remember MileWalker? we named its one method moveMile() we could have named the method move() and then redefined what “move” means to a MileWalker. Again, we’re modeling the real world. The concept of “move” is different depending on what type of object is “moving” (think about how a dog, fish, bird, etc., “move”) so, since the general concept is the same, we often use the same name (it makes coding easy/logical) - why would you want to try to remember moveMile(), moveLegs(), moveWings(), etc. - why not just one identifier for that - move() Ch. 4.1 - 4.3

Example 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 --- let’s see… Ch. 4.1 - 4.3

As always, the Big Picture first a.k.a. - Inheritance Hierarchy UrRobot MileWalker BackwardWalker DropBeeperAndWalker Ch. 4.1 - 4.3

MileWalker public class MileWalker extends UrRobot { // constructor same as always public void move() super.move(); super.move(); } heading needs to be identical to the one in the API for UrRobot in order for “overriding” to work Ch. 4.1 - 4.3

DropBeeperAndWalker public class DropBeeperAndWalker extends UrRobot { // constructor same as always public void move() putBeeper(); // inherited instruction still serves its purpose super.move(); } Ch. 4.1 - 4.3

BackwardWalker You write it! In addition to writing this class, write a sample Driver that would demonstrate using one robot each of type MileWalker, DropBeeperAndWalker, and BackwardWalker We’ll pick someone and put it up in 5 minutes… Ch. 4.1 - 4.3

Your sample Driver vs. mine a reference can refer to any object as long as the object is of the same type or a type of one of its subclasses somewhere down the Inheritance tree! UrRobot bot; bot = new MileWalker(…); bot.move(); // polymorphic move() bot = new DropBeeperAndWalker(…); bot.move(); // polymorphic move() bot = new BackwardWalker(…); Ch. 4.1 - 4.3

Polymorphism at run-time, the correct implementation is chosen depending on what specific object is being referenced at that moment in time. bot then later… instance of DropBeeperAndWalker then yet even later… instance of BackwardWalker instance of MileWalker Ch. 4.1 - 4.3

Polymorphism - cont’d polymorphism is ubiquitous (everywhere) in OOP there are many uses and examples of it let’s now build toward another example of polymorphism but first, as last time, we need some setup… Ch. 4.1 - 4.3

Choreographers one object controlling others we now want a MoveChoreographer class, which, when constructed, is passed 3 friends (robots) the MoveChoreographer has one method called moveFriends() which, when invoked, “moves” each friend once this Choreographer model of problem solving, by the way, can been seen in the “general contractor” analogy we used in the ppt from Ch. 3 - the general contractor doesn’t really do the work, she just delegates it to another object(s) Ch. 4.1 - 4.3

MoveChoreographer public class MoveChoreographer extends UrRobot { private UrRobot myBotA; private UrRobot myBotB; private UrRobot myBotC; // constructor on next slide // other methods } instance variables objects can not only do(behavior) things, they can also remember(state) things Ch. 4.1 - 4.3

MoveChoreographer’s constructor public MoveChoreographer ( int st, int av, Direction dir, int numBeepers, UrRobot botA, UrRobot botB, UrRobot botC ) { super (st, av, dir, numBeepers); // must come first in method myBotA = botA; myBotB = botB; myBotC = botC; } instance variables being assigned Ch. 4.1 - 4.3

MoveChoreographer’s moveFriends() public void moveFriends() { myBotA.move(); myBotB.move(); myBotC.move(); } Ch. 4.1 - 4.3

Sample Client code using a MoveChoreographer can you now give some sample client code that uses a MoveChoreographer object? (do so now for 5 minutes…) draw a picture and show the before and after an example: UrRobot bot1 = new MileWalker(2, 4, North, 0) ; UrRobot bot2 = new DropBeeperAndWalker(2, 5, North, infinity); UrRobot bot3 = new BackwardWalker(2, 6, North, 0); MoveChoreographer chor; chor = new MoveChoreographer(1, 1, North, 0, bot1, bot2, bot3); chor.moveFriends(); Ch. 4.1 - 4.3

examining the constructor’s reference types The statement from the previous slide, chor = new MoveChoreographer(1, 1, North, 0, bot1, bot2, bot3); is kind of neat. When someone constructs a MoveChoreographer, she can pass any 3 robots in any order as long as each one is-A UrRobot or extends from a UrRobot. The MoveChoreographer only wants to be guaranteed that it can perform a move() on any object passed to it - since there is a move() in UrRobot, it chose to make its parameters of type UrRobot, guaranteeing (to itself and the compiler) that it will be able to call move() at run-time.The term that describes which particular move() will be called at run-time is _________? Ch. 4.1 - 4.3

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… Ch. 4.1 - 4.3

contrived/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() discuss problems with design Ch. 4.1 - 4.3

BeeperLayers On the previous slide, we saw that layBeepers() would have the exact same implementation for both types of beeper layers - namely: { move(); putBeepers(); move(); } discuss why code duplication (a.k.a., copy/paste) and lack of localization are poor/costly design patterns Ch. 4.1 - 4.3

BeeperLayers At the same time, we saw that putBeepers() would have a different implementation in each of the subclasses (one puts 2, the other puts 3). So here is the new design pattern: We’ll extract out an abstract concept of what a general beeper layer would look like and put that into a class(in this case, an abstract class). Methods in the abstract class that would have the exact same implementation regardless of the subclass will be implemented in the abstract class - methods that would have different implementations in the subclasses will not be implemented in the abstract class, forcing each subclass to give its own unique implementation… Ch. 4.1 - 4.3

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

Terminology & Concepts 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(); making references to the code, the inheritance tree, or whatever else we just discussed in the BeeperLayer problem, pick one of these terms and demonstrate that you know what it means abstraction, abstract class, abstract method, polymorphism Ch. 4.1 - 4.3