Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comp1004: Object Oriented Design I Abstract Classes and Interfaces.

Similar presentations


Presentation on theme: "Comp1004: Object Oriented Design I Abstract Classes and Interfaces."— Presentation transcript:

1 Comp1004: Object Oriented Design I Abstract Classes and Interfaces

2 Coming up Abstract Classes and Methods The Challenges of Object Oriented Design – Pets vs. Animals Multiple Inheritance vs. Interfaces

3 Abstract Classes and Methods

4 Designing for Polymorphism ArrayList brokers; Brokers = new ArrayList (); brokers.add(new Broker(“pm@numberten.gov.uk”)); brokers.add(new Broker(“pres@france.gov.fr”)); 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

5 Designing for Polymorphism Broker 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…

6 Designing for Polymorphism Broker 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; }

7 Designing for Polymorphism Broker 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

8 Designing for Polymorphism Broker 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?

9 Abstract Classes 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 } 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

10 Abstract Classes 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 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 }

11 Abstract Methods public 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

12 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

13 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 methods abstract 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.

14 The Challenges of Object-Oriented Design

15 Back to an Animal Hierarchy AnimalCanineDogWolfFelineCatTigerRodentHamster We included methods for sleep, roam and eat in Animal In some subclasses these were overridden

16 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?

17 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 AnimalCanineDogWolfFelineCatTigerRodentHamster Add methods for stroke, feedTreat and play

18 Where could the play() method go? AnimalCanineDogWolfFelineCatTigerRodentHamster

19 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? AnimalCanineDogWolfFelineCatTigerRodentHamster Put methods in Animal class

20 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! AnimalCanineDogWolfFelineCatTigerRodentHamster Put methods in Animal class but mark them as abstract

21 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 AnimalCanineDogWolfFelineCatTigerRodentHamster Put methods in only the classes that need the behaviour

22 Any other ideas? Make your suggestions What can we do to solve this?

23 New Pet Class It sounds like we need a separate super class: AnimalCanineDogWolfFelineCatTigerRodentHamster Pet

24 Multiple Inheritance vs. Interfaces

25 Multiple inheritance In Java, multiple inheritance is not allowed This is because it gets hard to resolve clashes Class A Class B foo() Class B foo() Class C foo() Class C foo() Class D If foo() is called on an object of type D, which method gets called?

26 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

27 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

28 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)

29 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

30 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!

31 public interface Pet { void play(); } public interface CagedPet extends Pet { void takeOutOfCage(); void putInCage(); } public class Hamster extends Rodent implements CagedPet { //code omitted private 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

32 Summary Abstract Classes and Methods The Challenges of Object Oriented Design – Pets vs. Animals Multiple Inheritance vs. Interfaces


Download ppt "Comp1004: Object Oriented Design I Abstract Classes and Interfaces."

Similar presentations


Ads by Google