comp 121

21
COMP 121 Week 2: Interfaces and Polymorphism

Upload: hope-combs

Post on 31-Dec-2015

20 views

Category:

Documents


0 download

DESCRIPTION

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?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COMP 121

COMP 121

Week 2:Interfaces and Polymorphism

Page 2: COMP 121

Objectives

To learn about interfaces To be able to convert between class and

interface references To understand the concept of

polymorphism

Page 3: COMP 121

What is an Interface?

In general, an interface is something that facilitates interaction between two things

Examples:The buttons on a TV remoteThe controls in a carThe keypad, microphone, and speaker in a

phoneThe USB interface on a computer

Page 4: COMP 121

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.

Page 5: COMP 121

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 worldContract is enforced by the compiler

Page 6: COMP 121

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.

Page 7: COMP 121

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.

Page 8: COMP 121

Defining an Interface

public interface InterfaceName{

// Method signatures}

Page 9: COMP 121

Implementing an Interface

public class ClassNameimplements InterfaceName, InterfaceName,…

{// Implementation goes here

}

Page 10: COMP 121

Example: Predator Interface

public interface Predator{

public boolean pursuesPrey(Prey p);public void devoursPrey(Prey p);

}

Page 11: COMP 121

Example: Prey Interface

public interface Prey

{

public boolean fleesPredator(Predator p);

}

Page 12: COMP 121

Example: Lion Classpublic 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}

}

Page 13: COMP 121

Example: Frog Classpublic 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 }}

Page 14: COMP 121

Example: LargeCorp Classpublic 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 }}

Page 15: COMP 121

UML “is-a” Relationship

Page 16: COMP 121

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.

Page 17: COMP 121

To Cast or Not to Cast?

Lion simba = new Lion( );Frog kermit = new Frog( );Lion cat;

Predator attacker = simba; // no need to castcat = (Lion)attacker; // need to cast

attacker = kermit; // no need to cast

cat = (Lion)attacker; // what will happen?

Page 18: COMP 121

Example: Using Interfacespublic 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 }}

}

Page 19: COMP 121

Polymorphism

The principle that behavior can vary depending on the actual type of an object

Method selection takes place at runtime (late binding)

Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

Predator x = new Lion( );x.devoursPrey(y);

x = new Frog( );x.devoursPrey(z);

Page 20: COMP 121

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.

Page 21: COMP 121

Any Questions?