Object-Oriented Programming - Lecture 4: Interfaces - Lê Hồng Phương
● Defining an interface ● Implementing an interface ● Using an interface as a type ● Rewriting interfaces ● Examples
Bạn đang xem nội dung tài liệu Object-Oriented Programming - Lecture 4: Interfaces - Lê Hồng Phương, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Lecture 4: Interfaces
Lê Hồng Phương
phuonglh@gmail.com
Department of Mathematics, Mechanics and Informatics,
Vietnam National University, Hanoi
2012-2013 Object-Oriented Programming: Intefaces 2
Content
● Defining an interface
● Implementing an interface
● Using an interface as a type
● Rewriting interfaces
● Examples
2012-2013 Object-Oriented Programming: Intefaces 3
Defining an interface
● Interfaces are used to encode similarities which the
classes of various types share but do not necessarily
constitute a class relationship.
● Examples:
– A human and a parrot can both whistle. It does not
make sense to represent Human and Parrot as
subclasses of Whistle.
– A person and a rectangle can both measurable by
some criterion (e.g., by weight for persons, by area
for rectangles).
2012-2013 Object-Oriented Programming: Intefaces 4
Defining an interface
● In object-oriented languages, an interface is an
abstract type that is used to specify an interface (in
the generic sense of the term).
● An interface contains only
– method signatures (prototypes)
– constant declarations
public interface Relatable {
public int isLargerThan(Relatable other);
}
A purely abstract method.
Note the semicolon
2012-2013 Object-Oriented Programming: Intefaces 5
Defining an interface
● An interface can extends others:
public interface GroupedInterface extends
Interface1, Interface2, Interface3 {
double E = 2.718282;
void doSomething(int i, double x);
int doSomthingElse(String s);
}
A constant
Two method signatures
How many method signatures does the GroupedInterface have?
2012-2013 Object-Oriented Programming: Intefaces 6
Implementing an interface
● Interfaces can't be instantiated. We can't do this:
– Relatable relatable = new Relatable();
● Why?
– Because an interface is always an abstract type
and we can't create an abstract object.
● Interfaces are designed to be implemented by
classes.
2012-2013 Object-Oriented Programming: Intefaces 7
Implementing an interface
● A class implementing an interface must implement all
of the methods described in the interface.
● An abstract class implementing an interface may
provide implementations for some methods defined
by the interface.
– Methods that are not implemented are left to be
implemented in its subclasses.
2012-2013 Object-Oriented Programming: Intefaces 8
Implementing an interface
public class Person implements Relatable {
private double weight;
public Person(double w) {
weight = w;
}
public double getWeight() {
return weight;
}
public int isLargerThan(Relatable other) {
Person person = (Person) other;
if (this.getWeight() < person.getWeight())
return -1;
else if (this.getWeight() > person.getWeight())
return 1;
else
return 0;
}
}
Note the cast operator.
Implement the method
described in Relatable interface.
2012-2013 Object-Oriented Programming: Intefaces 9
Implementing an interface
public class Rectangle implements Relatable {
private int width = 0;
private int height = 0;
private Point origin;
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
public int getArea() {
return width * height;
}
public int isLargerThan(Relatable other) {
Rectangle otherRect = (Rectangle) other;
if (this.getArea() < otherRect.getArea())
return -1;
else if (this.getArea() > otherRect.getArea())
return 1;
else
return 0;
}
}
Implement the required method
to compare two rectangles.
2012-2013 Object-Oriented Programming: Intefaces 10
Using an interface
● An interface is used as a type.
● If you define a variable whose type is an interface,
any object you assign to it must be an instance of a
class that implements the interface.
Relatable person = new Person(50.5);
Relatable rectangle = new Rectangle(new Point(5, 5), 20, 40);
Relatable a;
a = new A();
This is not possible if class A
does not implement Relatable.
2012-2013 Object-Oriented Programming: Intefaces 11
Using an interface
● A method to find the largest object of two Relatable
objects:
public Object findLargest(Object object1, Object object2) {
Relatable obj1 = (Relatable) object1;
Relatable obj2 = (Relatable) object2;
if ((obj1).isLargerThan(obj2) > 0)
return object1;
else
return object2;
} This method may be used to
compare two persons or
two rectangles.
2012-2013 Object-Oriented Programming: Intefaces 12
Rewriting interfaces
● Consider an interface that you have developed:
public interface DoIt {
void doSomething(int i, double x);
int doSomethingElse(String s);
}
● Other users have provided concrete implementations
of this interface.
– They developed three classes A, B, C, all
implement DoIt.
2012-2013 Object-Oriented Programming: Intefaces 13
Rewriting interfaces
● Suppose that at a later time, you want to add a third
method to DoIt:
public interface DoIt {
void doSomething(int i, double x);
int doSomethingElse(String s);
boolean didItWork(int i, double x, String s);
}
● This makes all the classes implementing the interface
DoIt break. Classes A, B, C do not work anymore.
– Why? Because they don't implement the interface
anymore.
– Programmers rely on this interface will protest
loudly.
2012-2013 Object-Oriented Programming: Intefaces 14
Rewriting interfaces
● You should create a DoItPlus interface that extends
DoIt.
● Now users of your code can choose to continue to
use the old DoIt interface or upgrade to the new one.
public interface DoItPlus extends DoIt {
boolean didItWork(int i, double x, String s);
}
2012-2013 Object-Oriented Programming: Intefaces 15
More examples of interfaces
/**
* Describes an interface for any class whose
* objects can be measured.
*/
public interface Measurable {
/**
* Gets the measure of the object.
* @return a double
*/
double getMeasure();
}
Method documentation
Interface documentation
What should you do if you want to model something that
can be measured?
2012-2013 Object-Oriented Programming: Intefaces 16
More examples of interfaces
/**
* Describes any class whose objects
* can measure other objects.
*/
public interface Measurer {
/**
* Computes the measure of an object.
*
* @param anObject
* the object to be measured
*
* @return the measure of the object.
*/
double measure(Object anObject);
}
2012-2013 Object-Oriented Programming: Intefaces 17
More examples of interfaces
/**
* This interface imposes a total ordering on the objects of
* each class that implements it.
*/
public interface Comparable {
/**
* Compares this object with the specified object for order.
*
* @param o the object to be compared.
* @return a negative integer, zero, or a positive integer
* as this object is less than, equal to, or greater
* than the specified object.
*/
int compareTo(T o);
}
2012-2013 Object-Oriented Programming: Intefaces 18
More examples of interfaces
/**
* A predator can chase and eat preys.
*/
public interface Predator {
/**
* Chases a prey p.
* @param p a prey
* @return true if success,
false if failure.
*/
boolean chasePrey(Prey p);
/**
* Eats a prey.
* @param p a prey
*/
void eatPrey(Prey p);
}
2012-2013 Object-Oriented Programming: Intefaces 19
More examples of interfaces
/**
* A lion is a super predator.
*/
public class Lion implements Predator {
public boolean chasePrey(Prey p) {
// implement to chase prey p (specifically for a lion)
}
public void eatPrey(Prey p) {
// implement to eat prey p (specifically for a lion)
}
}
/**
* A frog can be a predator or a prey.
*/
public class Frog implements Predator, Prey {
// implement methods of the two interfaces here...
}
2012-2013 Object-Oriented Programming: Intefaces 20
More examples of interfaces
/**
* A venomous predator.
*/
public interface VenomousPredator implements Predator, Venomous {
// interface body
}
/**
* A snake is a venomous predator and it can be prey.
*/
public class Snake implements VenomousPredator, Prey {
// implement methods of the two interfaces here
}