390 likes | 400 Views
Learn about abstract classes, abstract methods, and class interfaces in Java programming. Understand how these concepts can help in code reuse and designing class hierarchies effectively.
E N D
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(); • 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!
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 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
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 HeadFirst Java • Don’t fear the oop (google for it) • Java tutorials • Chapter 8 in BlueJ
Coming up • A quick word about abstract • How a class interfaces • Making Pets • The Deadly Diamond of Death • Interfaces (not interfaces)
Do you remember? • Remember this animal hierarchy?
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?
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!
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
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!
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!
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:
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 foo() Class C foo() If foo() is called on an object of type D, which method gets called? Class D
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 dog1 eat Dog sleep bark
Dog has some inherited methods • And some specific methods • Now we want to add a method called Play roam toString play eat sleep bark
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 roam roam toString toString toString play play play eat eat eat sleep sleep sleep meow squeek bark Cat Hamster Dog
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)