94ee0f9f6b79839b2c24565c4302f9ae.ppt
- Количество слайдов: 39
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 Interfaces (not interfaces)
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
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.
public abstract void move(); No body! • 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
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
OO So what is the point? • It’s all about how your classes interact and what their interface is
Coming up • • • A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces)
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.
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.
The point • By putting abstract methods into your superclasses, you’ve guaranteed that the subclasses will 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
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
Suggested Reading • • Pg 165 onwards in Head. First Java Don’t fear the oop (google for it) Java tutorials Chapter 8 in Blue. J
Coming up • • • A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces)
A different way of guaranteeing behaviour
Do you remember? • Remember this animal hierarchy? Animal Canine Dog Feline Wolf Cat Rodent Tiger Hamster
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, feed. Treat() methods and some play() methods. • How should we do this?
• 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!
• Where could the play() method go? Animal Canine Dog Feline Wolf Cat Rodent Tiger Hamster
Option 1 • Put methods in Animal class • Pro – All animals inherit behaviour, no need to touch subclasses • Con Animal Canine Dog Feline Wolf Cat Rodent Tiger Hamster – We don’t really want to play with a tiger or stroke a wolf, this could be a dangerous choice
Option 2 • Pro • Put methods in – All animals inherit Animal class but mark behaviour, they them as abstract implement their own version at the first concrete subclass Animal Canine Dog • Con Feline Wolf Cat Rodent Tiger Hamster – 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!
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 Animal Canine Dog Feline Wolf Cat Rodent Tiger Hamster – 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!
Any other ideas? • Make your suggestions • What can we do to solve this?
New Pet Class • It sounds like we need a separate super class, Pet: Pet Animal Canine Dog Feline Wolf Cat Rodent Tiger Hamster
Coming up • • • A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces)
OO Multiple inheritance • In Java, multiple inheritance is not allowed • This is because of the deadly diamond of death! Class A Class B Class C foo() Class D If foo() is called on an object of type D, which method gets called?
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
Buttons Memory eat sleep bark dog 1 Dog
roam to. String play eat sleep bark • Dog has some inherited methods • And some specific methods • Now we want to add a method called Play
Coming up • • • A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces)
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
To define an interface public interface Pet{ public abstract void play(); }
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 }
• Implementing an interface is like adding an unprogrammed button to the remote roam to. String play eat eat sleep bark meow squeek Dog Cat Hamster
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
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
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 ; ) )
Summary • • • A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces)