1 / 22

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010. Interfaces (Part I). “ As far as the customer is concerned, the interface is the product . ” -- Jef Raskin.

reba
Download Presentation

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

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. CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Interfaces (Part I) “ As far as the customer is concerned, the interface is the product. ” --JefRaskin Course Lecture Slides9 June 2010 Ganesh Viswanathan

  2. Animal picture food sleep() roam() makeNoise() eat() Canine roam() Feline roam() Lion makeNoise() eat() Cat makeNoise() eat() Wolf makeNoise() eat() Dog makeNoise() eat()

  3. Problem • Need to add following methods to model Pet behaviors to the pet animal classes • play() • beFriendly()

  4. Option 1 • Add concrete methods: play() and beFriendly() to Animal class Animal Feline Canine Lion Cat Wolf Dog

  5. Option 2 • Add abstract methods: play() and beFriendly() to Animal class • Provide empty body for them in Lion and Wolf classes • Provide non-empty body for them in Cat and Dog classes Animal Feline Canine Lion Cat Wolf Dog

  6. Option 3 • Add concrete methods: play() and beFriendly() only in Cat and Dog classes Animal Feline Canine Lion Cat Wolf Dog

  7. What do we need? • Pet behaviors just in Pet classes like Dog and Cat • A way to guarantee that all Pet classes use the same signature for these Pet methods • A way to take advantage of Polymorphism

  8. What do we need? Animal Pet Feline Canine Lion Cat Wolf Dog But Java does not allow multiple inheritance! Why?

  9. DigitalRecorder CDBurner burn() DVDBurner() burn() ComboDrive Multiple Inheritance Issues

  10. Solution: Interfaces Make Pet an interface and put all pet behaviors in it as abstract methods Animal Pet abstract play(); abstract beFriendly(); Feline Canine Lion Cat Wolf Dog

  11. Interfaces • An interface is a classlike construct that • contains only constants and abstract methods. • cannot contain variables or concrete methods. • Syntax for declaring interfaces • public interface <InterfaceName> { • // constant declarations; • // method signatures; • }

  12. Pet Interface public interface Pet { public void beFriendly(); public void play(); }

  13. Interfaces, cont. • To implement an interface, a class must: • include the phrase implements <InterfaceName> at the start of the class definition 2. Provide body for all the methods listed in the definition of the interface or it must be declared abstract

  14. Implementing Pet Interface class Dog extends Canine implements Pet{ public void beFriendly(){ System.out.println(“Wag tail”); } public void play(){ System.out.println(“Catch ball”); } } class Cat extends Feline implements Pet{ public void beFriendly(){ System.out.println(“Curl up ”); } public void play(){ System.out.println(“Roll ball”); } }

  15. Using the Pet Interface class Main{ public static void main(String[] args){ Pet[] pets = new Pet[2]; pet[0] = new Dog(); pet[1] = new Cat(); for(int i = 0; i < 2; i++) pet[i].play(); } }

  16. Characteristics of Interfaces • Interfaces are defined in their own file • All methods of an interface are abstract. They do not have implementations. You may omit the abstract keyword. • Interfaces do not have instance fields. • Interfaces can have constant fields. These are automatically treated as public, static, final; you may omit any of these keywords

  17. Characteristics of Interfaces, cont. • A class can implement any number of interfaces by providing implementations for all the methods of all the interfaces it claims to implement. • Example: • public class A implements I1, I2, I3 { • … }

  18. Characteristics of Interfaces, cont. • Interfaces can not be instantiated • But can be used as a data type for a variable

  19. Classes from different inheritance hierarchy can implement same interface! Robot Animal Pet abstract play(); abstract beFriendly(); Feline Canine RoboDog Roomba Lion Cat Wolf Dog

  20. Interfaces vs. Abstract Classes

  21. Interfaces vs. Abstract Classes • You can not inherit from multiple classes but a class can implement multiple interfaces • Interfaces is used to model “can do” type relationships while Inheritance used to model “is a” type of relationship

  22. Get more info! • Interfaces @Java-doc: http://java.sun.com/docs/books/tutorial/java/IandI/createinterface.html • Why use interfaces? • http://www.codestyle.org/java/faq-Interface.shtml • Characteristics of interfaces: • http://mindprod.com/jgloss/interface.html

More Related