CSE201: Monsoon 2018, CSE Sec1on Advanced Programming: Lecture 05: Interfaces and Polymorphism
CSE201: Monsoon 2018, CSE Sec1on Advanced Programming: Lecture 05: Interfaces and Polymorphism
Advanced Programming
Lecture 05: Interfaces and
Polymorphism
Vivek Kumar
Computer Science and Engineering
IIIT Delhi
[email protected]
Last Lecture Interfaces in Java
● Group similar capabilities/function of different
classes together
● Interfaces can only declare methods - not define
them
● Interfaces are contracts that classes agree to
● If classes choose to implement given interface, it
must define all methods declared in interface
o if classes don’t implement one of interface’s
methods, the compiler raises error
Do we need two
different Racer classes??
Declaring an Interface Implementing an Interface
Any
similarities?
How about one Racer class with different methods?
@Override is an
@Override
annota2on – a signal to
the compiler (and to
anyone reading your
code)
This Lecture
4
Andries van Dam © 2016 9/22/16
Introducing Polymorphism
● Poly = many, morph = forms
● A way of coding generically
o way of referencing many related objects as one generic type
§ cars and bikes can both move() → refer to them as Transporter
objects
§ phones and camera can both getCharged() → refer to them as
Chargeable objects, i.e., objects that implement Chargeable interface
§ cars and mobile phones can both playRadio() → refer to them as
RadioPlayer objects
● How do we write one generic useTransportation()
method?
5
Andries van Dam © 2016 9/22/16
What would this look like in code?
public class Racer {
//previous code elided
public void useTransportation(Transporter transportation) {
transportation.move();
}
}
This is polymorphism!
transportation object passed
in could be instance of Car,
Bike, etc., i.e., any class that
implements the interface
6
Andries van Dam © 2016 9/22/16
Let’s break this down.
public class Racer {
//previous code elided
public void useTransportation(Transporter transportation) {
transportation.move();
}
}
7
Andries van Dam © 2016 9/22/16
Actual vs. Declared Type (1/2)
● Consider following piece of code:
8
Andries van Dam © 2016 9/22/16
Actual vs. Declared Type (2/2)
● Can treat Car/Bike object as
Transporter objects
● Car is the actual type
o Java will look in this class for the Transporter transportation = new Car();
definition of the method transportation.playRadio();
● Transporter is declared type
o Java will limit caller so it can only call
methods on instances that are
declared as Transporter objects
● If Car defines playRadio()
method. Is Nope. The playRadio() method
is not declared in Transporter
transportation.playRadio() interface, therefore Java does not
correct? recognize it as viable method call
9
Andries van Dam © 2016 9/22/16
Determining the Declared Type
● What methods do Car and Bike Bike implements Transporter
have in common? void move();
void dropKickstand();//etc.
o move()
● How do we know that?
o they implement Transporter Car implements Transporter
§ guarantees that they have move() void move();
method void playRadio();//etc.
● Think of Transporter like the
“lowest common denominator”
o it’s what all transportation classes
will have in common
10
Andries van Dam © 2016 9/22/16
Is this legal?
Transporter sophiasBike = new Bike();
11
Andries van Dam © 2016 9/22/16
Motivations for Polymorphism
● Many different kinds of transportation but only care about
their shared capability
o i.e. how they move
● Polymorphism let programmers sacrifice specificity for
generality
o treat any number of classes as their lowest common denominator
o limited to methods declared in that denominator
§ can only use methods declared in Transporter
● For this program, that sacrifice is ok!
o Racer doesn’t care if instance of Car can playRadio() or if instance of Bike
can dropKickstand()
o only method Racer wants to call is move() 12
Andries van Dam © 2016 9/22/16
Polymorphism in Parameters
● What are implications of this method declaration?
13
Andries van Dam © 2016 9/22/16
Is this legal?
Transporter sophiasBike = new Bike();
_sophia.useTransportation(sophiasBike); Even though
sophiasCar is
Car sophiasCar = new Car(); declared as a Car,
the compiler can still
_sophia.useTransportation(sophiasCar); verify that it
implements
Radio sophiasRadio = new Radio(); Transporter.
_sophia.useTransportation(sophiasRadio);
16
Andries van Dam © 2016 9/22/16
Method Resolution: Which move() is executed?
● Consider this line of code in Race class:
_sophia.useTransportation(new Bike());
21
Andries van Dam © 2016 9/22/16
Clicker Question
Given the following class:
public class Laptop implements Typeable, Clickable {
public void type() {
// code elided
}
public void click() {
//code elided
}
Given
that typeable has declared the type method and clickable has declared
the click method, which of the following calls is/are valid?
Declared
23
Andries van Dam © 2016 9/22/16
Why does that work? (2/2)
● Declared type and actual type work together
o declared type keeps things generic
§ can reference a lot of objects using one generic type
o actual type ensures specificity
§ when defining implementing class, the methods can get implemented
in any way
Bike Actual
This is my
Transporter object!
Declared
24
Andries van Dam © 2016 9/22/16
When to use polymorphism?
● Using only functionality declared in interface or specialized
functionality from implementing class?
o if only using functionality from the interface à polymorphism!
o if need specialized methods from implementing class, don’t use
polymorphism
25
Andries van Dam © 2016 9/22/16
Why use interfaces?
● Contractual enforcement
o will guarantee that class has certain capabilities
§ Car implements Transporter, therefore it must know how to move()
● Polymorphism
o Can have implementation-agnostic classes and methods
§ know that these capability exists, don’t care how they’re implemented
§ allows for more generic programming
• useTransportation can take in any Transporter object
• can easily extend this program to use any form of transportation, with
minimal changes to existing code
§ an extremely powerful tool for extensible programming
26
Andries van Dam © 2016 9/22/16
Why is this important?
● With 2 modes of transportation!
● Old Design:
o need more classes à more specialized methods
(useRollerblades(), useBike(), etc)
● New Design:
o as long as the new classes implement Transporter, Racer
doesn’t care what transportation it has been given
o don’t need to change Racer!
§ less work for you!
§ just add more transportation classes that implement Transporter
27
Andries van Dam © 2016 9/22/16
public class Racer {
29
Andries van Dam © 2016 9/22/16
Next Lecture
● Inheritance and polymorphism
30