Download COMP 121 Week 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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?
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
Java Interface Type

Declares a set of methods and their signatures



Interface is similar to a class but contains only methods
and constants




Methods are used to communicate with an object
Methods form the object's interface with the outside world
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.
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
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.
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.
Defining an Interface
public interface InterfaceName
{
// Method signatures
}
Implementing an Interface
public class ClassName
implements InterfaceName, InterfaceName,…
{
// Implementation goes here
}
Example: Predator Interface
public interface Predator
{
public boolean pursuesPrey(Prey p);
public void devoursPrey(Prey p);
}
Example: Prey Interface
public interface Prey
{
public boolean fleesPredator(Predator p);
}
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
}
}
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
}
}
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
}
}
UML “is-a” Relationship
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.
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?
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
}
}
}
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.
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.
Any Questions?