38
Andries van Dam 2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all students in CS15 were not yet comfortable using Java at the beginning of the course Only 1% have used the JavaFX graphics library (which you will learn about next week) some programming experience a lot of programming experience no prior coding experience

Andries van Dam 2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Embed Size (px)

Citation preview

Page 1: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/151/38

Initial Questionnaire – The Results Are In!

• 56% of all students in CS15 have no prior coding experience!

• 81% of all students in CS15 were not yet comfortable using Java at the beginning of the course

• Only 1% have used the JavaFX graphics library (which you will learn about next week)

some programming experience

a lot of programming experience

no prior coding experience

Page 2: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/152/38

Review: Constructors

● Parentheses after constructor, not in class declaration

● The constructor (called by another instance) makes the SamBot– do not “new” an object of the same class in the constructor

public class SamBot {private int _numsteps;

public SamBot() {_numsteps = 5;

}}

public class SamBot() {private int _numsteps;public SamBot{

_numsteps = 5;new SamBot();

}}

public class SamBot {private int _numsteps;public SamBot(new SamBot()) {

_numsteps = 5;}

}

Correct Incorrect Incorrect

● Constructor could take in parameters – but not a new SamBot

Page 3: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/153/38

Review: Assigning Variables

• What is the output of this program?1050

• Remember: in Java, = is an operator for variable assignmento This is different from the

mathematical notion of equality!

public class NumberPrinter {

public NumberPrinter(){ this.printNumbers();}

public void printNumbers(){ int a = 1;

int b = 10; a = b;

b = 50; System.out.println(a);

System.out.println(b); }}

Page 4: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/154/38

Review: Parameter Passingpublic class DogGroomer {

public DogGroomer() {// this is the

constructor!}

public void groom(Dog shaggyDog) {

// code that grooms shaggyDog}

}

public class PetShop {

/* This is the constructor! */public PetShop() {

Dog dogE = new Dog();DogGroomer groomer = new

DogGroomer();groomer.groom(dogE);

}

}

● DogGroomer’s groom method is defined here, and takes in a parameter of type Dog

● shaggyDog is the name used by groom to reference the Dog that is passed in

● Calling the groom method requires passing in an existing Dog as argument

● Existing Dog is dogE, and Java will substitute dogE for shaggyDog every place the groom method refers to it

Page 5: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/155/38

Review: Method Signatures

● The same signature can occur in multiple classes (e.g., Cat and Dog) without ambiguity – the type of the instance on which the call will be made lets Java disambiguate

● If a class is a subclass of another, and both have the same method signature(s), then overriding occurs – method resolution disambiguates

● If there is no inheritance relationship between the classes with the same signature, there is no overriding, and the methods each work independently

● This applies to methods in support code classes as well!

public class Dog extends Animal {

public Dog() {}

public void eat(Food f){

this.wolfItDown(f);}

}

public class Cat extends Animal {

public Cat() {}

public void eat(Food f){

this.eatDelicately(f);this.WipePaws();

}}

public abstract class Animal {

public Animal() {}

public abstract void eat(Food f);

}

Page 6: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/156/38

Review: Types of VariablesType of Variable: Instance Local No Variable

When to use it: If you need to refer to it throughout the class

If you only need to refer to it inside of a method

When you never need to refer to it in the future after construction within the scope of the class in which it was created (i.e., created and passed as an argument)

Syntax: _variable variable Somewhere in andy.teach(){ … _andy.takeWater(new H2OBottle()); …}

Note: The underscore notation is only a CS15 style convention for instance variables – it doesn’t affect how Java interprets your code

Page 7: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/157/38

Lecture 7Polymorphism

Page 8: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/158/38

Introduction

● Inheritance review

● What is polymorphism?

● Advantages of polymorphism

● Polymorphism with interfaces

● Limitations of polymorphism

Page 9: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/159/38

Coding Generically● We’ve seen this abstraction pattern over and over so far…

o classes and instances: factor out common capabilities of instances into a class

o parameters: write a generic method that behaves differently based on input

o inheritance: factor out common capabilities of similar classes into superclass

o interfaces: factor out common capabilities of dissimilar classes into interface

● This lecture: Polymorphism! Generic coding to the max

Page 10: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1510/38

Inheritance Review

● Convertible, CS15Mobile, and Van are all subclasses of Vehicle

● Subclasses inherit properties and capabilities from their superclass

● Subclass may choose to override a method declared in the superclass to further specialize that capability

● Thus, subclasses may all respond to the same method call differently (e.g., move in their own way)

Vehicle

Convertible CS15Mobile Van

Inheritance Diagram

Page 11: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1511/38

Polymorphism (1/6)

● Polymorphism is a fancy word for “multiple forms”

● The big idea: we can refer to an instance of a subclass as if it were an instance of its superclass

Page 12: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1512/38

Polymorphism (2/6)

● Here we have a Racetrack class. It’s going to set up races between three Vehicles

● We want our Racetrack to be generic, so that any kind of Vehicle can race

● We declare our three Vehicles with type Vehicle

public class Racetrack {

private Vehicle _vehicle1;private Vehicle _vehicle2;private Vehicle _vehicle3;

public Racetrack() {_vehicle1 = new Van();_vehicle2 = new Convertible();_vehicle3 = new CS15Mobile();

}}

Page 13: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1513/38

Polymorphism (3/6)● In the constructor of

Racetrack, we instantiate our Vehicles

● Vehicle is an abstract class-- cannot instantiate it directly

● Instead, we instantiate a Van, a Convertible, and a CS15Mobile, and assign them to _vehicle1, _vehicle2, and _vehicle3

● Vehicle thus takes on three different forms: instances of its subclasses

public class Racetrack {

private Vehicle _vehicle1;private Vehicle _vehicle2;private Vehicle _vehicle3;

public Racetrack() {_vehicle1 = new Van();_vehicle2 = new

Convertible();_vehicle3 = new

CS15Mobile();}

}

Page 14: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1514/38

Polymorphism (4/6)● In this example, declared type

of _vehicle1, _vehicle2, and _vehicle3 is Vehicle

● But _vehicle1’s actual type is Van, _vehicle2’s actual type is Convertible, and _vehicle3’s actual type is CS15Mobile

● Thus, _vehicle1 is an instance of Van, but we’re referring to it as if it were just a generic Vehicle

public class Racetrack {

private Vehicle _vehicle1;private Vehicle _vehicle2;private Vehicle _vehicle3;

public Racetrack() {_vehicle1 = new Van();_vehicle2 = new

Convertible();_vehicle3 = new

CS15Mobile();}

}

Page 15: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1515/38

Polymorphism (5/6)● Now we’ve written a race

method. When called, it tells each Vehicle to move

● What happens?

o _vehicle1 is a Van, so it moves slowly

o _vehicle2 is a Convertible, so it moves fast

o _vehicle3 is a CS15Mobile, so it moves at moderate speed

public class Racetrack {

private Vehicle _vehicle1;private Vehicle _vehicle2;private Vehicle _vehicle3;

public Racetrack() {_vehicle1 = new Van();_vehicle2 = new Convertible();_vehicle3 = new CS15Mobile();

}

public void race() {_vehicle1.move();_vehicle2.move();_vehicle3.move();

}}

Page 16: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1516/38

Polymorphism (6/6)● Each object responds to method

calls as specified by its actual type – method resolution

● race doesn’t care what types of Vehicle it’s dealing with

● If we wanted to race different types of Vehicle, we would only have to modify the constructor

● Good generic programming, but not that impressive...

● The real power of polymorphism is still to come!

public class Racetrack {

private Vehicle _vehicle1;private Vehicle _vehicle2;private Vehicle _vehicle3;

public Racetrack() {_vehicle1 = new Van();_vehicle2 = new

Convertible();_vehicle3 = new CS15Mobile();

}

public void race() {_vehicle1.move();_vehicle2.move();_vehicle3.move();

}}

Page 17: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1517/38

Without Polymorphism (1/2)

● Here’s a Driver class

● All Drivers have the ability to drive a Convertible

● But there are other kinds of Vehicles out there… We want Drivers to be able to drive Vans and CS15Mobiles too!

● Let’s tackle the problem without using polymorphism

public class Driver {

// constructor elided

public void driveConvertible(Convertible c) {

c.move();}

}

Page 18: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1518/38

Without Polymorphism (2/2)

● This works! We just wrote a different method for each kind of Vehicle

● But what if we want to introduce more kinds of Vehicle, like Trucks and SUVs?

● Would need to keep writing new methods for each type of Vehicle we might ever want a Driver to drive!

public class Driver {

// constructor elided

public void driveConvertible(Convertible c) {

c.move();}

public void driveCS15Mobile(CS15Mobile cm) {

cm.move();}

public void driveVan(Van v) {v.move();

}}

Page 19: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1519/38

With Polymorphism! (1/3)

● This is exactly the problem polymorphism was designed to solve

● Instead of declaring many different methods, each with a parameter of a different declared type, can use single method that takes in any Vehicle!

public class Driver {

// constructor elided

public void drive(Vehicle vehicle) {vehicle.move();

}}

Page 20: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1520/38

With Polymorphism! (2/3)● In a generic drive(), specify that the

declared type of the parameter is Vehicle

● Caller will pass in an instance of a specific subclass (like CS15Mobile) as argument

● subclass type is actual type, a subtype of the declared type

● Thus, generic method refers to instance of subclass as if it were an instance of its superclass…if you pass in a CS15Mobile, its specific move() method will be called – polymorphic parameter passing!

public class Driver {

// constructor elided

public void drive(Vehicle vehicle) {vehicle.move();

}}

Page 21: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1521/38

With Polymorphism! (3/3)

● Here’s an example of calling the drive method, passing in a different type of Vehicle as an argument each time

● Can pass in an instance of any subclass of Vehicle!

● And method resolution will invoke the method for the subclass!

● Relaxation of ultra-strict type matching

public class DriversEdApp {

Driver _studentDriver;

public DriversEdApp() {_studentDriver = new Driver();

}

public void startDrivingTest() {_studentDriver.drive(new

Convertible());_studentDriver.drive(new

CS15Mobile());_studentDriver.drive(new Van());

}}

Page 22: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1522/38

Polymorphism has to be used carefully...

● Consider the Zookeeper class, which has the method feedLion

● feedLion can take in any Lion

● Let’s say we have the following code in another class:

Cat cat = new Lion();

● What is the declared type? The actual type?

● Can we pass cat into the feedLion method?

o no!

public class Zookeeper {

// constructor elided

public void feedLion(Lion lion) {// implementation elided}

}

Page 23: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1523/38

Polymorphism is Tricky...● feedLion will only accept an

argument of type Lion, and can call any Lion methods on its parameter

● cat is declared as type Cat - as far as Java knows, could be any kind of Cat! (Tiger, Panther, etc.), and Cats won’t know all Lion methods!

● The argument’s declared type must be the same class, or a subclass, of the parameter’s declared type

● What‘s an easy fix (if you didn’t actually use any Lion-specific methods?

public class Zookeeper {

// constructor elided

public void feedLion(Lion lion) {// implementation elided}

}

Page 24: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1524/38

Polymorphism With Interfaces (1/4)

● Polymorphism with interfaces looks much the same as it does with classes

● Imagine that the classes CS15Mobile and Watch both implement the Repairable interface

public interface Repairable {

public void repair();

}

Page 25: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1525/38

Polymorphism With Interfaces (2/4)

● Let’s say we have a RepairShop class

● The RepairShop has the method fix, which takes in any object that implements the interface Repairable

public class RepairShop {

// constructor elided

public void fix(Repairable brokenThing) {

brokenThing.repair();}

}

Page 26: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1526/38

Polymorphism With Interfaces (3/4)

● Notice that we use the interface name as the declared type of the parameter

● Interfaces can be used as types of variables and parameters, just like classes can!

public class RepairShop {

// constructor elided

public void fix(Repairable brokenThing) {

brokenThing.repair();}

}

Page 27: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1527/38

Polymorphism With Interfaces (4/4)

● Just like with superclasses and subclasses, object passed in will respond to method call specifically based on its actual type

● With polymorphism, interfaces are truly useful-- can write a single method to deal with any object that fulfills a certain contract

public class RepairShop {

// constructor elided

public void fix(Repairable brokenThing) {

brokenThing.repair();}

}

Page 28: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1528/38

Limitations of Polymorphism (1/2)● Let’s go back to the Racetrack

example

● We know that _vehicle2 is actually a Convertible

● Let’s say that Convertibles have a method called putTopDown

● Not all Vehicles can do this, but Convertibles can

● Is race() allowed to call “_vehicle2.putTopDown();”?

public class Racetrack {

private Vehicle _vehicle1;private Vehicle _vehicle2;private Vehicle _vehicle3;

public Racetrack() {_vehicle1 = new Van();_vehicle2 = new

Convertible();_vehicle3 = new CS15Mobile();

}

public void race() {_vehicle1.move();_vehicle2.move();_vehicle3.move();

}}

Page 29: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1529/38

Limitations of Polymorphism (2/2)● No!

● Can only call methods that are defined in (or inherited by) the declared type of the object

● We’re referring to _vehicle2 as a generic Vehicle - can only tell it to do things that all Vehicles know how to do

● Same premise applies with interfaces - if declared type is an interface, can only call methods defined in that interface

public class Racetrack {

private Vehicle _vehicle1;private Vehicle _vehicle2;private Vehicle _vehicle3;

public Racetrack() {_vehicle1 = new Van();_vehicle2 = new

Convertible();_vehicle3 = new CS15Mobile();

}

public void race() {_vehicle1.move();_vehicle2.move();_vehicle3.move();

}}

Page 30: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1530/38

Is This Code Correct? (1/7)

● Let’s say that FordVan is a subclass of Van, and both are concrete classes

● Will this code compile?

● No!

public class CarDealership {

private FordVan _myFordVan;

public CarDealership() {_myFordVan = new Van();

}}

Page 31: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1531/38

● Polymorphism doesn’t work backwards!

● Since FordVan is the declared type of _myFordVan, we should be able to call any of FordVan’s methods on it

● A generic Van wouldn’t know how to respond to FordVan’s specialized methods!

● A FordVan “is a” Van, but a Van is typically not a FordVan

public class CarDealership {

private FordVan _myFordVan;

public CarDealership() {_myFordVan = new Van();

}}

Is This Code Correct? (2/7)

Page 32: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1532/38

● Assume that Cat and Bird are both subclasses of Animal

● Will this code compile?

● No!

public class Cat extends Animal {

// constructor elided

public void catchBird(Bird unluckyBird) {

// implementation elided}

}

public class BirdCatchingApp {

public BirdCatchingApp() {Cat sylvester = new Cat();Animal tweety = new Bird();

sylvester.catchBird(tweety);}

}

Is This Code Correct? (3/7)

Page 33: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1533/38

● The Cat’s catchBird method takes in a Bird as a parameter

● But we’ve declared tweety as an Animal, meaning we must refer to tweety as only an Animal

● catchBird is declared to allow calls to Bird-specific methods on whatever argument is passed in, but we can only allow methods that are common to all Animals to be called on tweety

public class Cat extends Animal {

// constructor elided

public void catchBird(Bird unluckyBird) {

// implementation elided}

}

public class BirdCatchingApp {

public BirdCatchingApp() {Cat sylvester = new Cat();Animal tweety = new Bird();

sylvester.catchBird(tweety);}

}

Is This Code Correct? (4/7)

Page 34: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1534/38

● We can fix this by just declaring tweety as a Bird!

public class Cat extends Animal {

// constructor elided

public void catchBird(Bird unluckyBird) {

// implementation elided}

}

public class BirdCatchingApp {

public BirdCatchingApp() {Cat sylvester = new Cat();Bird tweety = new Bird();

sylvester.catchBird(tweety);}

}

Is This Code Correct? (5/7)

Page 35: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1535/38

public class Zoo {

private Monkey _monkey;

public Zoo() {_monkey = new Monkey();Fruit mango = new Mango();Apple apple = new Apple();_monkey.eat(mango);_monkey.eat(apple);_monkey.eat(new Banana());

}}

public class Monkey {

// constructor elided

public void eat(Fruit fruit) {fruit.beDigested();

}}

● Assume that Mango, Apple and Banana are subclasses of Fruit

Is This Code Correct? (6/7)

Page 36: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1536/38

public class Zoo {

private Monkey _monkey;

public Zoo() {_monkey = new Monkey();Fruit mango = new Mango();Apple apple = new Apple();_monkey.eat(mango);_monkey.eat(apple);_monkey.eat(new Banana());

}}

public class Monkey {

// constructor elided

public void eat(Fruit fruit) {fruit.beDigested();

}}

Yes, this code works!

Is This Code Correct? (7/7)

Page 37: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1537/38

That’s It!● Polymorphism is the ultimate tool for coding generically!

o method takes in a parameter of type <superclass> or <interface> (e.g. Vehicle or Repairable)

o result: can pass instance of any subclass into that method!

● Limitation: When calling methods on an object, can only ever call methods of its declared type!o if myCar is declared as a Vehicle, can’t call putTopDown on it,

even if it’s actually a Convertible

● Polymorphism thus lets instances of subclasses and interface-implementing classes become “multiple forms” of the generic parameter or variable

Page 38: Andries van Dam  2015 10/01/15 1/38 Initial Questionnaire – The Results Are In! 56% of all students in CS15 have no prior coding experience! 81% of all

Andries van Dam 2015 10/01/1538/38

Announcements

● LiteBrite on-time handin is tonight at 11:59PMo Late handin is Saturday 10/3 at 10pm

● TA SafeHouse is released todayo Design questions are due Sunday 10/4 2PM

● Today is the last day to get checked off for lab 1 at TA hours