1 / 39

Coming up

Coming up. A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces). OO. Lecture 15. Interface this. Coming up. A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death

pbaird
Download Presentation

Coming up

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. Coming up • A quick word about abstract • How a class interfaces • Making Pets • The Deadly Diamond of Death • Interfaces (not interfaces)

  2. OO Lecture 15 Interface this

  3. Coming up • A quick word about abstract • How a class interfaces • Making Pets • The Deadly Diamond of Death • Interfaces (not interfaces)

  4. OO A quick word about abstract • You looked at abstract in the last lab • A quick review • Mark classes that you don’t want instantiated to be abstract. Subclasses can still be instantiated • An abstract class has little to no use unless it is extended • A non-abstract class, the ones we normally deal with are called concrete classes

  5. OO Abstract methods • You can also mark methods as abstract • If you have a method that doesn’t make sense unless it is in a more specific subclass, then mark it abstract. A method like move() – can you define a general way for everything to move? • An abstract class must be extended • An abstact method must be overridden • An abstract method has no body.

  6. public abstract void move(); • If you declare an abstract method... • ... You must mark the class as abstract • You cannot have an abstract method in a concrete class • Buy you can mix abstract and non abstract methods in an abstract class No body!

  7. If you do use abstract methods • You have to implement any abstract methods in a superclass in the first concrete class(es) down the inheritance tree • That means you must provide a method body in the first concrete class that inherits from the abstract class OO

  8. OO So what is the point? • It’s all about how your classes interact and what their interface is

  9. Coming up • A quick word about abstract • How a class interfaces • Making Pets • The Deadly Diamond of Death • Interfaces (not interfaces)

  10. OO How a class interfaces • We say a class’ interface is basically what public methods it provides. • By using these public methods, an other class can interact with it • A class’ public methods are a bit like buttons another class can press. • We’ve shown this by using a remote control as a metaphor.

  11. Code reuse • Code reuse is a big thing in programming • All/most of the Java Library code is very helpful and very useful • It pays to make your classes reuseable.

  12. The point • By putting abstract methods into your superclasses, you’ve guaranteed that the subclasseswill provide certain methods • This makes big projects easier. You know exactly what methods you can definitely call from any subclasses of a common superclass • It also makes reusing code easier too

  13. End of inheritance • That is the end of formal teaching of inheritance. • Inheritance is BIG • it is important • Many programming languages make heavy use of it. • But....it is hard to understand at first

  14. Suggested Reading • Pg 165 onwards in HeadFirst Java • Don’t fear the oop (google for it) • Java tutorials • Chapter 8 in BlueJ

  15. Coming up • A quick word about abstract • How a class interfaces • Making Pets • The Deadly Diamond of Death • Interfaces (not interfaces)

  16. A different way of guaranteeing behaviour

  17. Do you remember? • Remember this animal hierarchy?

  18. 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?

  19. Buzz Groups • With 4 or so people around you: • 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 • Smart Alecs – don’t mention the i-word, you’ll ruin the surprise!

  20. Where could the play() method go?

  21. Option 1 • Put methods in Animal class • Pro • All animals inherit behaviour, no need to touch subclasses • Con • We don’t really want to play with a tiger or stroke a wolf, this could be a dangerous choice

  22. Option 2 • Put methods in Animal class but mark them as abstract • Pro • All animals inherit behaviour, they implement their own version at the first concrete subclass • Con • You’d have to sit and implement behaviour in all those low level classes, even the ones you don’t want to have the methods in!

  23. Option 3 • Put methods in only the classes that need the behaviour • Pro • Only animals you want to have the methods will have them • Con • You’d have to agree an exact protocol for pet methods with anyone who will ever use your class • You couldn’t call any of the pet methods if you were treating the object as type Animal – Animal doesn’t have those specific methods!

  24. Any other ideas? • Make your suggestions • What can we do to solve this?

  25. New Pet Class • It sounds like we need a separate super class, Pet:

  26. Coming up • A quick word about abstract • How a class interfaces • Making Pets • The Deadly Diamond of Death • Interfaces (not interfaces)

  27. OO Multiple inheritance • In Java, multiple inheritance is not allowed • This is because of the deadly diamond of death! Class A Class B foo() Class C foo() If foo() is called on an object of type D, which method gets called? Class D

  28. OO On a quest for a solution • Lets think a bit more about class interfaces. • The class interface is the public methods it provides • Like the buttons on the remote

  29. Buttons Memory dog1 eat Dog sleep bark

  30. Dog has some inherited methods • And some specific methods • Now we want to add a method called Play roam toString play eat sleep bark

  31. Coming up • A quick word about abstract • How a class interfaces • Making Pets • The Deadly Diamond of Death • Interfaces (not interfaces)

  32. OO Interfaces • We can add obligations for specific methods with an Interface • An Interface is a 100% abstract class • Like a class • But no implimentation

  33. To define an interface public interface Pet{ public abstract void play(); }

  34. To implement an interface public class Dog extends Canine implements Pet{ String name; //Dog code here public void play(){ System.out.println(“Dog is playing”); } //more Dog code here }

  35. Implementing an interface is like adding an unprogrammed button to the remote roam roam roam toString toString toString play play play eat eat eat sleep sleep sleep meow squeek bark Cat Hamster Dog

  36. Programming the button • By implementing the interface you are adding the unprogrammed button • You have to program the button in the class that implements the interface • Otherwise the compiler grumbles

  37. And this is useful because...? • Lots of reasons! • You know that everything that implements an interface will have those methods that you call • They don’t even need to be from the same inheritance tree – lets say you had a recyclable interface, you could have an inheritance tree of metals and another of plastics, but some types of both could be recyclable • If someone wants to add a class to your program, you don’t need to give them the superclass, just make them implement the common interface and the class will fit in just fine

  38. Also... • Classes can extend only one class • But they can implement many interfaces • So Dog can be a Pet • And it can be Comparable (allows collections to sort objects) • And it can be Serializable (allows Java to ‘deflate’ the object to save it for when you next run the program) • (Useful for saving your leveled up Dark Mystical Elf character ;) )

  39. Summary • A quick word about abstract • How a class interfaces • Making Pets • The Deadly Diamond of Death • Interfaces (not interfaces)

More Related