Download presentation
Presentation is loading. Please wait.
Published byColin McCoy Modified over 7 years ago
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
A quick word about abstract
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
Abstract methods OO 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
How a class interfaces OO
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 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
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? Animal Canine Dog
Wolf Feline Cat Tiger Rodent Hamster
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?
Animal Canine Dog Wolf Feline Cat Tiger Rodent Hamster
21
Option 1 Put methods in Animal class Pro Con
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 Animal Canine Dog Wolf Feline Cat Tiger Rodent Hamster
22
Option 2 Pro Put methods in Animal class but mark them as abstract Con
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! Put methods in Animal class but mark them as abstract Animal Canine Dog Wolf Feline Cat Tiger Rodent Hamster
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! Put methods in only the classes that need the behaviour Animal Canine Dog Wolf Feline Cat Tiger Rodent Hamster
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: Pet
Animal Canine Dog Wolf Feline Cat Tiger Rodent Hamster
26
Coming up A quick word about abstract How a class interfaces
Making Pets The Deadly Diamond of Death Interfaces (not interfaces)
27
Multiple inheritance OO 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
On a quest for a solution
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 eat sleep bark Memory dog1 Dog
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.