90 likes | 291 Views
I have wonderful news, mother. Joe finally implemented all his abstract methods! Now everything is working just the way we planned…. Interfaces and Abstract Classes. Abstract vs. Concrete. Abstract. Animal. Concrete. Canine. Hippo. Feline. Dog. Wolf. Cat. Lion. Tiger.
E N D
I have wonderful news, mother. Joe finally implemented all his abstract methods! Now everything is working just the way we planned… Interfaces and Abstract Classes
Abstract vs. Concrete Abstract Animal Concrete Canine Hippo Feline Dog Wolf Cat Lion Tiger
Abstract Classes • You can not make a new instance of an abstract class! Abstract public class Canine extends Animal { public void roam() { } } • An abstract class means the class must be extended
Abstract Methods public abstract void eat(); • There is not a method body, end it with a semicolon • An interface includes abstract methods • An abstract method means it must be overridden It really sucks to be an abstract method. You don’t have a body.
Making an Interface • A Java interface is like a 100% pure abstract class! • To define an Interface: Public interface Pet { public abstract void beFriendly(); public abstract void play(); } • All methods in an interface are abstract, so any class that is – A Pet MUST implement (i.e. override) the methods of Pet.
Showing the Pet Interface Pet Animal Canine Hippo Feline Dog Wolf Cat Lion Tiger
Implementing an Interface public class Dog extends Canine implements Pet { public void beFriendly() {……} public void play() {……} public void roam() {……} public void eat() {…….} } You must implement the pet methods! These are just normal overriding methods.
Forget about animals! • Flier Interface • What abstract methods would be needed for classes that fly • Classes that implement the flier interface could be: • Airplane • Ski jumper • ???? • Write out the interface and the classes that implement it.
Instance Variables Does it make sense to say a Tub IS-A bathroom? Or a Bathroom IS-A Tub? Well it doesn’t to me. The relationship between my Tub and my Bathroom is HAS-A. Bathroom HAS-A Tub. That means Bathroom has a Tub instance variable.