object-oriented programming: interfaces & constructors · soquotablespecifies the apifor any...

26
. . . . . . . . . Object-Oriented Programming: Interfaces & Constructors Ewan Klein School of Informatics Inf1 :: 2009/10 Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 1 / 25

Upload: phunglien

Post on 15-Jun-2019

217 views

Category:

Documents


0 download

TRANSCRIPT

. . . . . .

.

.

. ..

.

.

Object-Oriented Programming:Interfaces & Constructors

Ewan Klein

School of Informatics

Inf1 :: 2009/10

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 1 / 25

. . . . . .

.. .1 Interfaces

.. .2 Constructors

.. .3 Admin

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 2 / 25

. . . . . .

Greeting V. 1, 1

Class Talker gets a greeting string from an object h of class Hello.Then sends a greeting to each friend.

.Talker

..

.

. ..

.

.

public class Talker {private String[] friends = { ”Jack”, ”Ann” };public void makeGreeting(Hello h) {

for (String friend : friends) {System.out.printf(”%s, %s!\n”, h.sayHello(), friend);

}}public static void main(String[] args) {

Hello h = new Hello();Talker t = new Talker();t.makeGreeting(h);

}}

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 3 / 25

. . . . . .

UML Class Diagram

Solid line with open arrow tip denotes an association relationship. I.e., an instanceof Talker uses a Hello object.

makeGreeting()

friends

Talker

sayHello()

Hello

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 4 / 25

. . . . . .

Greeting V. 1, 2

Hello class is trivial:.Hello..

.

. ..

.

.

public class Hello {public String sayHello() {

return ”Hello”;}

}

.Output..

.

. ..

.

.

Hello, Jack!Hello, Ann!

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 5 / 25

. . . . . .

Extending Greeting V. 1

Suppose we decide to go international, and add a new class Bonjour.

This is pretty much the same as Hello, but uses a different method name andreturns a different string.

.Bonjour

..

.

. ..

.

.

public class Bonjourpublic String ditBonjour()

return ”Bonjour”;

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 6 / 25

. . . . . .

Possible Solution?

Hello and Bonjour should both be usable by TalkerBut we can’t do this straightforwardly; so create a new ‘French’ version?

.TalkerFrench

..

.

. ..

.

.

public class TalkerFrench {private String[] friends = { ”Jacques”, ”Annette” };public void makeGreeting(Bonjour b) {

for (String friend : friends) {System.out.printf(”%s, %s!\n”, b.ditBonjour(), friend);

}}public static void main(String[] args) {

Bonjour b = new Bonjour();TalkerFrench t = new TalkerFrench();t.makeGreeting(b);

}}

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 7 / 25

. . . . . .

Greeting V. 1.5, 1

Building TalkerFrench to use Bonjour is wasteful — we’re duplicating codein Talker.

Can we get a more general version of Talker which can use both Hello andBonjour?

Step 1: give both these classes have a common API; i.e., they use same thesame methods.

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 8 / 25

. . . . . .

Greeting V. 1.5, 2

.Hello V. 1.5

..

.

. ..

.

.

public class Hello {public String greet() {

return ”Hello”;}

}

.Bonjour V. 1.5

..

.

. ..

.

.

public class Bonjour {public String greet() {

return ”Bonjour”;}

}

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 9 / 25

. . . . . .

Greeting V. 2, 1

How do we say, in general, what the shared API is?

For example, how do we enforce that a new class BuonGiorno conforms tothis API?

Step 2: Abstract the API into a Java interface.

.Quotable Interface

..

.

. ..

.

.

public interface QuotableString greet(); //public by default//Look! No body to this method!

So Quotable specifies the API for any class that is going to be usable by our newversion of Talker.

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 10 / 25

. . . . . .

Introducing Interfaces

How do we enforce that a new class BuonGiorno conforms to this API?Step 3: Insist that any conformant class implements the Quotable interface.

.Hello V. 2

..

.

. ..

.

.

public class Hello implements Quotable {public String greet() {

return ”Hello”;}

}.BuonGiorno..

.

. ..

.

.

public class BuonGiorno implements Quotable {public String greet() {

return ”Buon Giorno”;}

}

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 11 / 25

. . . . . .

UML Class Diagram

Dotted line with closed arrow tip denotes an is-a relationship between a class andan interface. I.e. Hello is an implementation of the Quotable interface.

greet(): String

<<interface>>Quotable

Hello BuonGiornoBonjour

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 12 / 25

. . . . . .

Refactoring Talker

How do we restructure — “refactor” — Talker to use objects that implementQuotable?

Step 4: Use Quotable as a type.

That is, we can declare variables like this:Quotable q;

No objects actually have type Quotable, but we do have objects of classesthat implement Quotable, namely:

I objects of class Hello, Bonjour, BuonGiornoq = new Hello();

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 13 / 25

. . . . . .

Greeting V. 2, 2.Talker V. 2..

.

. ..

.

.

public class Talker {private String[] englishFriends = { ”Jack”, ”Ann” };private String[] frenchFriends = { ”Jacques”, ”Annette” };public void makeGreeting( Quotable q , String[] friends) {

for (String friend : friends) {System.out.printf(”%s, %s!\n”, q.greet(), friend);

}}public static void main(String[] args) {

Quotable q1 = new Hello();Quotable q2 = new Bonjour();Talker t = new Talker();t.makeGreeting(q1, t.englishFriends);t.makeGreeting(q2, t.frenchFriends);

}}

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 14 / 25

. . . . . .

Polymorphism

Consider this signature:

public void makeGreeting( Quotable q , String[] friends)

We have an object q, but we don’t know what class it is.

So what can we do with it?

Answer: we can invoke any methods of the interface:String s = q.greet()

If q is a reference to a Hello object, we call Hello.greet(); if it’s a referenceto a Bonjour object, we call Bonjour.greet().

Example of polymorphism: a uniform treatment of objects that implementdifferent behaviours.

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 15 / 25

. . . . . .

Output from Greeting V. 2

.Output..

.

. ..

.

.

Hello, Jack!Hello, Ann!Bonjour, Jacques!Bonjour, Annette!

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 16 / 25

. . . . . .

Errors to Avoid, 1

.Incorrect Hello..

.

. ..

.

.

public class Hello implements Quotable {String greet() { // should be public!

return ”Hello”;}

}

Methods in an interface are public by default, but not in an ordinary class.

So example above is wrong; greet() has to be declared public.

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 17 / 25

. . . . . .

Errors to Avoid, 2

.Incorrect use of Quotable type.... ..

.

.Quotable q = new Quotable(); // interfaces cannot be instantiated!

You cannot construct an object of type Quotable.

Interfaces are not classes.

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 18 / 25

. . . . . .

Constructors, 1

.Instantiating Bicycle.... ..

.

.Bicycle bike = new Bicycle();

Code that gets run whenever you invoke new.

Can be used to initialize state of object.

Here Bicycle has a no-argument constructor.

But constructors can have arguments.

Suppose a Rectangle is determined by x and y coordinates, together with widthand height:.Make a Rectangle.... ..

.

.Rectangle box = new Rectangle(5, 10, 20, 30);

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 19 / 25

. . . . . .

Constructors, 2

When you write.Class definition..

.

. ..

.

.

public class Bicycle {...

}

Java compiler adds a default constructor:.Class definition with Constructor..

.

. ..

.

.

public class Bicycle {

public Bicycle() {...}

...}

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 20 / 25

. . . . . .

Constructors, 2

When you write.Class definition..

.

. ..

.

.

public class Bicycle {...

}

Java compiler adds a default constructor:.Class definition with Constructor..

.

. ..

.

.

public class Bicycle {

public Bicycle() {...}

...}

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 20 / 25

. . . . . .

Constructors, 3

Constructor name (e.g. Bicycle) is the same as the class name.

NB No return type — constructor looks like a method, but isn’t one!

Write the constructor(s) after instance variables, before methods.

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 21 / 25

. . . . . .

Make Your Own Constructor, 1

.Declared Constructor..

.

. ..

.

.

public class Bicycle {private int gears;private String brand;

public Bicycle(int gears, String brand) { //2 arg constructorthis.gears = gears;this.brand = brand;

System.out.printf(”Bike: gears=%d, brand=%s\n”, gears, brand);}

}

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 22 / 25

. . . . . .

Constructors, 4

If you write a constructor with arguments, you might expect compiler to addthe no-arg constructor.

Not so: you have to write it yourself.

No-arg constructor often defined in terms of constructor with arguments.

.No-arg Constructor..

.

. ..

.

.

public Bicycle() {this (15, ”Cannondale”);

}

If you have more than one constructor in a class, they must have differentargument lists.

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 23 / 25

. . . . . .

Make Your Own Constructor, 2

.BikeLauncher

..

.

. ..

.

.

public class BikeLauncherpublic static void main(String[] args)

Bicycle bike1 = new Bicycle(10, ”Trek”);Bicycle bike2 = new Bicycle();

.Output..

.

. ..

.

.

Bike: gears=10, brand=TrekBike: gears=15, brand=Cannondale

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 24 / 25

. . . . . .

Admin, Reading, etc

Assessed Exercise:I mostly will be marked by your tutor;I this week’s tutorial is opportunity to discuss general issues with the exercise;I hope to have marked answers available for collection by end of week; ITO will

notify.

Interfaces: HFJ, pp 224–226

Constructors: HFJ, pp 240–247

Exam environment: normal DICE environment except firewalled down.

All applications should work exactly as normal; e.g., gedit, emacs, Eclipse,NetBeans, etc.

You won’t have your normal home directory however —- so any localconfiguration and setup will not be available in the exam.

Ewan Klein (School of Informatics) Object-Oriented Programming: Interfaces & Constructors Inf1 :: 2009/10 25 / 25