1 / 45

COP 3503 FALL 2012 Shayan Javed Lecture 6

COP 3503 FALL 2012 Shayan Javed Lecture 6. Programming Fundamentals using Java. Interfaces. Animal picture food sleep() roam() makeNoise () eat(). Animal picture food sleep() roam() makeNoise () eat(). Canine roam(). Feline roam(). Animal picture food sleep() roam()

rowena
Télécharger la présentation

COP 3503 FALL 2012 Shayan Javed Lecture 6

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. COP 3503 FALL 2012ShayanJavedLecture 6 Programming Fundamentals using Java

  2. Interfaces

  3. Animal picture food sleep() roam() makeNoise() eat()

  4. Animal picture food sleep() roam() makeNoise() eat() Canine roam() Feline roam()

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

  6. Problem • Need to add Pet behaviors to the pet animal classes (Cat and Dog): • play()

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

  8. Option 1 • Add concrete methods: play() to Animal class • What’s the problem? Animal Feline Canine Lion Cat Wolf Dog

  9. Option 1 • Add concrete methods: play() to Animal class • What’s the problem? • Also exist for Lion/Wolf Animal Feline Canine Lion Cat Wolf Dog

  10. Option 2 • Add abstract methods: play() to Animal class Animal Feline Canine Lion Cat Wolf Dog

  11. Option 2 • Add abstract methods: play() to Animal class • Provide empty body for it in Lion and Wolf classes Animal Feline Canine Lion Cat Wolf Dog

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

  13. Option 3 • Add concrete methods: play() to Cat and Dog classes Animal Feline Canine Lion Cat Wolf Dog

  14. What we need • Pet behaviors just in Pet classes (like Dog and Cat)

  15. What we need • Pet behaviors just in Pet classes (like Dog and Cat) • Consistency - Guarantee that all Pet classes use the same signature for Pet methods

  16. What we need • Pet behaviors just in Pet classes (like Dog and Cat) • Consistency - Guarantee that all Pet classes use the same signature for Pet methods • Take advantage of Polymorphism

  17. Multiple Inheritance? Animal Pet Feline Canine Lion Cat Wolf Dog

  18. Multiple Inheritance? Animal Pet Feline Canine Lion Cat Wolf Dog But Java does not allow multiple inheritance! Why?

  19. Multiple Inheritance? • Not allowed • Mainly to avoid complexity/confusion. • Want to keep it simple.

  20. SuperClass method() A method() B method() C The Diamond Problem

  21. SuperClass method() A method() B method() C The Diamond Problem What happens when method() is called in C?

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

  23. Interfaces • Classlike construct • contains only constants and abstract methods.

  24. Interfaces • Classlike construct • contains only constants and abstract methods. • cannot contain variables or concrete methods.

  25. Interfaces • Classlike construct • contains only constants and abstract methods. • cannot contain variables or concrete methods. • Declaration: public interface <InterfaceName> { // constant declarations; // method signatures; }

  26. Interface Pet publicinterface Pet { publicvoid play(); }

  27. Implementing Pet interface class Dog extends Canine implements Pet{ publicvoid play(){ System.out.println(“Catch ball”); } }

  28. Implementing Pet interface class Dog extends Canine implements Pet{ publicvoid play(){ System.out.println(“Catch ball”); } } class Cat extends Feline implements Pet { publicvoid play(){ System.out.println(“Roll ball”); } }

  29. Interface Characteristics • Interfaces are defined in their own file

  30. Interface Characteristics • Interfaces are defined in their own file • Methods are abstract. No implementations. • May omit the abstract keyword.

  31. Interface Characteristics • Interfaces are defined in their own file • Methods are abstract. No implementations. • May omit the abstract keyword. • Do not have instance fields.

  32. Interface Characteristics • Interfaces are defined in their own file • Methods are abstract. No implementations. • May omit the abstract keyword. • Do not have instance fields. • Can have constant fields. Automatically treated as public, static, final; may omit any of these keywords

  33. Example publicinterface Interface1 { intaNumber = 0; publicvoidaMethod(); }

  34. Example publicinterface Interface1 { intaNumber = 0; publicvoidaMethod(); } Same as: publicinterface Interface1 { public static final intaNumber = 0; publicabstractvoidaMethod(); }

  35. Interface Characteristics • A class can implement any number of interfaces public class aClassimplements I1, I2, I3 { // methods from I1, I2, I3... }

  36. Interface Characteristics • A class can implement any number of interfaces public class aClassimplements I1, I2, I3 { // methods from I1, I2, I3... } • Interfaces can not be instantiated

  37. Interface Characteristics • A class can implement any number of interfaces public class aClassimplements I1, I2, I3 { // methods from I1, I2, I3... } • Interfaces can not be instantiated • But can be used as a data type for a variable

  38. Using the interface Pet[] pets = new Pet[2]; pet[0] = new Dog(); pet[1] = new Cat(); for(inti = 0; i < pets.length; i++) pet[i].play();

  39. Using the interface Pet[] pets = new Pet[2]; pet[0] = new Dog(); pet[1] = new Cat(); for(inti = 0; i < pets.length; i++) pet[i].play(); Similar to abstract classes – advantage of polymorphism

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

  41. Abstract Classes vs. Interfaces

  42. Abstract Classes vs. Interfaces

  43. Abstract Classes vs. Interfaces

  44. Abstract Classes vs. Interfaces • Cannot inherit from multiple classes but, • Can can implement multiple interfaces • Interfaces = model “can-do” type relationships • Inheritance = model “is-a” type of relationship

  45. Next Lecture • More on interfaces

More Related