1 / 9

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…. Interfaces and Abstract Classes. Abstract vs. Concrete. Abstract. Animal. Concrete. Canine. Hippo. Feline. Dog. Wolf. Cat. Lion. Tiger.

imaran
Download Presentation

Interfaces and Abstract Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

More Related