1 / 21

COMP 121

COMP 121. Week 2: Interfaces and Polymorphism. Objectives. To learn about interfaces To be able to convert between class and interface references To understand the concept of polymorphism. What is an Interface?.

saul
Télécharger la présentation

COMP 121

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. COMP 121 Week 2: Interfaces and Polymorphism

  2. Objectives • To learn about interfaces • To be able to convert between class and interface references • To understand the concept of polymorphism

  3. What is an Interface? • In general, an interface is something that facilitates interaction between two things • Examples: • The buttons on a TV remote • The controls in a car • The keypad, microphone, and speaker in a phone • The USB interface on a computer

  4. Java Interface Type • Declares a set of methods and their signatures • Methods are used to communicate with an object • Methods form the object's interface with the outside world • Interface is similar to a class but contains only methods and constants • Cannot contain instance variables • Specifies the methods that must be supported by any class that implements the interface • Cannot be instantiated • Methods are implicitly defined as public and abstract • The body of the method is not filled in • The body or implementation of the method is in the classes that implement the interface Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  5. Interface is like a Contract • Allows a class to be more formal about the behavior it promises to provide • Forms a contract between the class and the outside world • Contract is enforced by the compiler

  6. Classes that Implement an Interface • Use the implements keyword to indicate they implement the interface type • Must include all methods defined by the interface • Interface methods being implemented must be declared as public methods Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  7. Why Use an Interface? • Makes code more general and reusable • Reduces coupling between classes • Insures all classes that implement the interface will have a certain behaviors because interface methods must be implemented Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  8. Defining an Interface public interface InterfaceName { // Method signatures }

  9. Implementing an Interface public class ClassName implements InterfaceName, InterfaceName,… { // Implementation goes here }

  10. Example: Predator Interface public interface Predator { public boolean pursuesPrey(Prey p); public void devoursPrey(Prey p); }

  11. Example: Prey Interface public interface Prey { public boolean fleesPredator(Predator p); }

  12. Example: Lion Class public class Lion implements Predator { public boolean pursuesPrey(Prey p) { // Implementation of method goes here } public void devoursPrey(Prey p) { // Implementation of method goes here } public void roars( ) { // Implementation of method goes here } }

  13. Example: Frog Class public class Frog implements Predator, Prey { public boolean pursuesPrey(Prey p) { // Implementation of method goes here } public void devoursPrey(Prey p) { // Implementation of method goes here } public boolean fleesPredator(Predator p) { // Implementation of method goes here } }

  14. Example: LargeCorp Class public class LargeCorp implements Predator { public boolean pursuesPrey(Prey p) { // Implementation of method goes here } public void devoursPrey(Prey p) { // Implementation of method goes here } public double maximizeProfits( ) { // Implementation of method goes here } }

  15. UML “is-a” Relationship

  16. Converting Between Class and Interface Types • You can convert from a class type to an interface type, provided the class implements the interface • Interface type  Class type • Variables whose type is an interface must be cast when assigned to a variable whose type is a class that implements the interface • Class type  Interface type • Variables whose type is a class that implements the interface do not have to be cast when assigned to a variable whose type is the interface Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  17. To Cast or Not to Cast? Lion simba = new Lion( ); Frog kermit = new Frog( ); Lion cat; Predator attacker = simba; // no need to cast cat = (Lion)attacker; // need to cast attacker = kermit; // no need to cast cat = (Lion)attacker; // what will happen?

  18. Example: Using Interfaces public class ZooKeeper { public boolean areHostile(Predator pred, Prey prey) { if ((pred.pursuesPrey(prey)) || (prey.fleesPredator(pred))) { return true; // are hostile } else { return false; // are not hostile } } }

  19. Polymorphism • The principle that behavior can vary depending on the actual type of an object • Method selection takes place at runtime (late binding) Predator x = new Lion( ); x.devoursPrey(y); x = new Frog( ); x.devoursPrey(z); Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  20. Summary • Use interface types to make code more reusable • A Java interface type declares a set of methods and their signatures • An interface type has no implementation • The implements keyword indicates that a class implements an interface • Interfaces reduce coupling between classes • You can convert from a class type to an interface type as long as the class implements the interface • A cast is needed to convert from an interface type to a class type • Polymorphism is the principle that behavior can vary depending on the actual type of an object Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  21. Any Questions?

More Related