comp1004: object oriented design i

32
Comp1004: Object Oriented Design I Abstract Classes and Interfaces

Upload: clint

Post on 24-Feb-2016

40 views

Category:

Documents


0 download

DESCRIPTION

Comp1004: Object Oriented Design I. Abstract Classes and Interfaces. Coming up. Abstract Classes and Methods The C hallenges of Object Oriented Design Pets vs. Animals Multiple Inheritance vs. Interfaces. Abstract Classes and Methods. Designing for Polymorphism. Broker. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Comp1004: Object Oriented Design I

Comp1004: Object Oriented Design IAbstract Classes and Interfaces

Page 2: Comp1004: Object Oriented Design I

Coming up

• Abstract Classes and Methods

• The Challenges of Object Oriented Design– Pets vs. Animals

• Multiple Inheritance vs. Interfaces

Page 3: Comp1004: Object Oriented Design I

Abstract Classes and Methods

Page 4: Comp1004: Object Oriented Design I

Designing for PolymorphismArrayList<Broker> brokers;Brokers = new ArrayList<Broker>();

brokers.add(new Broker(“[email protected]”));brokers.add(new Broker(“[email protected]”));brokers.add(new SMSBroker(“07123 456789”));brokers.add(new SnailBroker(“1600 Penns Ave.

D.C.”));brokers.add(new SMSBroker(“07987 654321”));

String message = “Reminder. Fix economy!”);

for(Broker b : brokers) {b.sendMsg(message);

}

Broker

email

sendMsg

SMSBroker

number

sendMsg

SnailBroker

address

sendMsg

Page 5: Comp1004: Object Oriented Design I

Designing for PolymorphismBroker

email

sendMsg

SMSBroker

number

sendMsg

SnailBroker

address

sendMsg

A design that takes advantage of Polymorphism – but there is something odd going on. Think about the constructors…

Page 6: Comp1004: Object Oriented Design I

Designing for PolymorphismBroker

email

sendMsg

SMSBroker

number

sendMsg

SnailBroker

address

sendMsg

A design that takes advantage of Polymorphism – but there is something odd going on. Think about the constructors…

public class Broker {String email;//code omittted

public Broker(String emailaddr) {email = emailaddr;}

}

public class SMSBroker extends Broker {String number;//code omittted

public SMSBroker(String phoneno) {super(“Doesn’t matter – this is not used”);number = phoneno;}

}

Page 7: Comp1004: Object Oriented Design I

Designing for PolymorphismBroker

sendMsg

SMSBroker

number

sendMsg

SnailBroker

address

sendMsg

A better design would be to have a neutral Broker, and make EmailBroker another sub-class…

EmailBroker

email

sendMsg

Now there is no redundant data in the sub-classes

Page 8: Comp1004: Object Oriented Design I

Designing for PolymorphismBroker

sendMsg

SMSBroker

number

sendMsg

SnailBroker

address

sendMsg

A better design would be to have a neutral Broker, and make EmailBroker another sub-class…

EmailBroker

email

sendMsg

Now there is no redundant data in the sub-classes

But what does this method do?

It has to be here for Polymorphism to work.

But in fact does it make sense to have an instance of a Broker at all?

Page 9: Comp1004: Object Oriented Design I

Abstract Classespublic abstract class Broker {

public void sendMsg(String msg) {//exists so it can be//overriden in a subclass}

}

public class SMSBroker extends Broker {String number;//code omittted

public void sendMsg(String msg) {//some code to send the//msg to the phoneno}

}

Abstract classes cannot be instantiated (i.e. created using the new keyword)

They exist only to be extended

A non-abstract class (the ones we normally deal with) are called concrete classes

Page 10: Comp1004: Object Oriented Design I

Abstract ClassesAbstract classes cannot be instantiated (i.e. created using the new keyword)

They exist only to be extended

A non-abstract class (the ones we normally deal with) are called concrete classes

But this is still ugly – there is no guarantee that sub-classes will override this empty method!

public abstract class Broker {

public void sendMsg(String msg) {//exists so it can be//overriden in a subclass}

}

public class SMSBroker extends Broker {String number;//code omittted

public void sendMsg(String msg) {//some code to send the//msg to the phoneno}

}

Page 11: Comp1004: Object Oriented Design I

Abstract Methodspublic abstract class Broker {

public abstract void sendMsg(String msg);

}

public class SMSBroker extends Broker {String number;//code omittted

public void sendMsg(String msg) {//some code to send the//msg to the phoneno

}}

Abstract classes cannot be instantiated (i.e. created using the new keyword)

They exist only to be extended

A non-abstract class (the ones we normally deal with) are called concrete classes

So we can declare sendMsg as an abstract method – this means all concrete sub-classes must override it

Page 12: Comp1004: Object Oriented Design I

The point

• By putting abstract methods into your superclasses, you’ve guaranteed that the concrete subclasses will provide them

• This enables you to code for polymorphism without having to write methods that contain no code or will never be called

Page 13: Comp1004: Object Oriented Design I

Abstract Summary• An abstract class is a class that is declared abstract—it may or may not include abstract

methods. Abstract classes cannot be instantiated, but they can be subclassed.

• An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void sendMsg(String msg);

• If a class includes abstract methods, the class itself must be declared abstract, as in:

public abstract class Broker {// declare fields// declare non-abstract methodsabstract void sendMsg(String msg);

}

• When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.

Page 14: Comp1004: Object Oriented Design I

The Challenges of Object-Oriented Design

Page 15: Comp1004: Object Oriented Design I

Back to an Animal Hierarchy

Animal

Canine

Dog Wolf

Feline

Cat Tiger

Rodent

Hamster

We included methods for sleep, roam and eat in Animal

In some subclasses these were overridden

Page 16: Comp1004: Object Oriented Design I

Re-using this code

• What if we wanted to reuse this code to model someone’s pets?

• We want to implement some new stroke() methods, feedTreat() methods and some play() methods.

• How should we do this?

Page 17: Comp1004: Object Oriented Design I

• In groups of no more than four…

• Come up with some places in the hierarchy that we could add those methods to the appropriate animals. Think about using abstract methods in some cases

• Come up with the good points and the drawbacks for each location

Animal

Canine

Dog Wolf

Feline

Cat Tiger

Rodent

Hamster

Add methods for stroke, feedTreat and play

Page 18: Comp1004: Object Oriented Design I

• Where could the play() method go?

Animal

Canine

Dog Wolf

Feline

Cat Tiger

Rodent

Hamster

Page 19: Comp1004: Object Oriented Design I

Option 1

• All animals inherit behaviour, no need to touch subclasses

• But what does it mean to play with an Animal? Surely we would play differently with a Dog than a Hamster?

Animal

Canine

Dog Wolf

Feline

Cat Tiger

Rodent

Hamster

• Put methods in Animal class

Page 20: Comp1004: Object Oriented Design I

Option 2• All animals inherit

behaviour, they implement their own version at the first concrete subclass

• But does it make sense to play will all types of Animal? It doesn’t sound very safe to play with a Tiger!

Animal

Canine

Dog Wolf

Feline

Cat Tiger

Rodent

Hamster

• Put methods in Animal class but mark them as abstract

Page 21: Comp1004: Object Oriented Design I

Option 3• Only animals you want to

have the methods will have them

• You can’t use Polymorphism at all – as no superclasses have the play method

• So you’d have to write specific code for each subtype of Animal that you could play with

Animal

Canine

Dog Wolf

Feline

Cat Tiger

Rodent

Hamster

• Put methods in only the classes that need the behaviour

Page 22: Comp1004: Object Oriented Design I

Any other ideas?

• Make your suggestions

• What can we do to solve this?

Page 23: Comp1004: Object Oriented Design I

New Pet Class

• It sounds like we need a separate super class:

Animal

Canine

Dog Wolf

Feline

Cat Tiger

Rodent

Hamster

Pet

Page 24: Comp1004: Object Oriented Design I

Multiple Inheritance vs. Interfaces

Page 25: Comp1004: Object Oriented Design I

Multiple inheritance

• In Java, multiple inheritance is not allowed• This is because it gets hard to resolve clashes

Class A

Class B

foo()

Class C

foo()

Class DIf foo() is called on an object of type D, which method gets called?

Page 26: Comp1004: Object Oriented Design I

Interfaces

• Java provides Interfaces to allow you to solve this problem without introducing any of the awkward questions about clashing methods

• An Interface behaves like a 100% abstract class– i.e. containing only abstract methods

• A Class can only extend one other Class, but it can implement many Interfaces

• An Interface is like a contract, any class that implements that Interface guarantees to provide code for its methods

Page 27: Comp1004: Object Oriented Design I

public interface Pet{void play();

}

public class Dog extends Canine implements Pet {//code omitted

public void play(){System.out.println(“Dog plays with a ball”);

}}

public class Cat extends Feline implements Pet {//code omitted

public void play(){System.out.println(“Cat plays with some string”);

}}

public class Hamster extends Rodent implements Pet {//code omitted

public void play(){System.out.println(“Hamster plays on its wheel”);

}}

The interface declaration looks very like a class. All methods are automatically public and abstract

Remember that each class can extend only one superclass, but can implement many interfaces

Page 28: Comp1004: Object Oriented Design I

public void playWithAnyTwoPets(Pet petOne, Pet petTwo){System.out.println(“Lets play with our pets”);

petOne.play();petTwo.play();

}

public static void main(String [ ] args) {Dog dog = new Dog(“Fido”);Cat cat = new Cat(“Mr Tiddles”);Hamster hamster = new Hamster(“Hammy”);

playWithAnyTwoPets(dog, cat);playWithAnyTwoPets(cat, hamster);playWithAnyTwoPets(hamster, dog);

}

Elsewhere in our code we can use interfaces anywhere where we might use a class

Here it is used as a parameter – so this method will accept any class that implements the Pet interface

We cannot create an instance of a Pet, however we can create instances of classes that implement Pet

And pass them as a parameter when a Pet is expected (just like passing a subclass when a superclass is expected)

Page 29: Comp1004: Object Oriented Design I

Extend vs. Implements

• Extend means to add additional behaviour or data (methods or member variables)

• Implements means to provide implementation for a set of methods

• A Class can extend one other class• A Class can implement many Interfaces

Page 30: Comp1004: Object Oriented Design I

Extend vs. Implements

• Extend means to add additional behaviour or data (methods or member variables)

• Implements means to provide implementation for a set of methods

• A Class can extend one other class• A Class can implement many Interfaces

• An Interface can also extend many other Interfaces!

Page 31: Comp1004: Object Oriented Design I

public interface Pet {void play();

}

public interface CagedPet extends Pet {void takeOutOfCage();void putInCage();

}

public class Hamster extends Rodent implements CagedPet {//code omittedprivate boolean inCage = true;

public void play() {if(inCage)

System.out.println(“Hamster plays on its wheel”);else

System.out.println(“Hamster trundles in its ball”);}

public void takeOutOfCage() {inCage = false;

}

public void putInCage () {inCage = true;

}

}

Here CagedPet extends Pet which means that classes that implement CagedPet must provide all three methods

Each class that implements CagedPet is free to implement those methods however it sees fit

Page 32: Comp1004: Object Oriented Design I

Summary

• Abstract Classes and Methods

• The Challenges of Object Oriented Design– Pets vs. Animals

• Multiple Inheritance vs. Interfaces