1 / 32

Comp1004: Object Oriented Design I

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.

clint
Download Presentation

Comp1004: Object Oriented Design I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 Broker ArrayList<Broker> brokers; Brokers = new ArrayList<Broker>(); 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); } email sendMsg SMSBroker SnailBroker number address sendMsg sendMsg

  5. Designing for Polymorphism A design that takes advantage of Polymorphism – but there is something odd going on. Think about the constructors… Broker email sendMsg SMSBroker SnailBroker number address sendMsg sendMsg

  6. Designing for Polymorphism A design that takes advantage of Polymorphism – but there is something odd going on. Think about the constructors… Broker 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; } } email sendMsg SMSBroker SnailBroker number address sendMsg sendMsg

  7. Designing for Polymorphism A better design would be to have a neutral Broker, and make EmailBroker another sub-class… Broker Now there is no redundant data in the sub-classes sendMsg SMSBroker EmailBroker SnailBroker number email address sendMsg sendMsg sendMsg

  8. Designing for Polymorphism A better design would be to have a neutral Broker, and make EmailBroker another sub-class… Broker Now there is no redundant data in the sub-classes sendMsg 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? SMSBroker EmailBroker SnailBroker number email address sendMsg sendMsg sendMsg

  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 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 But this is still ugly – there is no guarantee that sub-classes will override this empty method! A non-abstract class (the ones we normally deal with) are called concrete classes

  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 So we can declare sendMsg as an abstract method – this means all concrete sub-classes must override it A non-abstract class (the ones we normally deal with) are called concrete classes

  12. The point • By putting abstract methods into your superclasses, you’ve guaranteed that the concrete subclasseswill 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 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. Add methods for stroke, feedTreat and play • 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

  18. Where could the play() method go?

  19. Option 1 • Put methods in Animal class • 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?

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

  21. Option 3 • Put methods in only the classes that need the behaviour • 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

  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:

  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 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. 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 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”); } }

  28. 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 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); } 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. Here CagedPet extends Pet which means that classes that implement CagedPet must provide all three methods public interface Pet { void play(); } public interface CagedPetextends Pet { void takeOutOfCage(); void putInCage(); } public class Hamster extends Rodent implements CagedPet { //code omitted private booleaninCage = 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; } } 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

More Related