Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces and Abstract Classes

Similar presentations


Presentation on theme: "Interfaces and Abstract Classes"— Presentation transcript:

1 Interfaces and Abstract Classes
I have wonderful news, mother. Joe finally implemented all his abstract methods! Now everything is working just the way we planned…

2 Abstract vs. Concrete Abstract Animal Concrete Canine Hippo Feline Dog
Wolf Cat Lion Tiger

3 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

4 It really sucks to be an abstract method. You don’t have a body.
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.

5 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.

6 Showing the Pet Interface
Animal Canine Hippo Feline Dog Wolf Cat Lion Tiger

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

8 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.

9 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.


Download ppt "Interfaces and Abstract Classes"

Similar presentations


Ads by Google