04 OOP InterfacesAndInheritance
04 OOP InterfacesAndInheritance
04 OOP InterfacesAndInheritance
July 2012
1
Interfaces Inheritance
// method signatures
void doSomething(int i, double x);
A class can extend only one other class, an interface can extend any
number of interfaces.
public Person(double w) {
}
weight = w;
Implement the
public double getWeight() {
return weight;
required method
}
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 casting
}
4
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);
}
If you make this change, all classes that implement the old DoIt
interface will break because they dont implement the interface
anymore.
Now users of your code can choose to continue to use the old interface
or upgrade to the new one.
/** 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
*/
double measure(Object anObject);
}
9
Interfaces Inheritance
10
In Java, excepting Object, every class has one and only one direct
superclass (single inheritance).
A subclass inherits all the members (fields, methods, and nested classes)
from its superclass.
java.lang.Object is the
base class of all Java
objects.
12
13
void printStates() {
System.out.println("cadence:" + cadence + " speed:" + speed + " gear:"
+ gear);
}
}
14
15
If the subclass is in the same package as its parent, it also inherits the package-private
members of the parent.
If you declare a field in the subclass with the same name as the one in the
superclass --> hide it (not recommended)
If you write a new instance method that has the same signature as the one in the
superclass --> override it.
If you write a new static method that has the same signature as the one in the
superclass --> hide it.
A subclass does not inherit the private member of its parent class.
A nested class has access to all the private members of its enclosing
class--both fields and methods.
17
Casting shows the use of an object of one type in place of another type.
Note that the access modifier for an overriding method can allow more,
but not less, access than the overridden method.
19
Subclasses of a class can define their own unique behaviors and yet
share some of the same functionality of the parent class.
22
23
You make a method final if it has an implementation that should not be changed and
is critical to the consistent state of the object.
class ChessAlgorithm {
enum ChessPlayer {WHITE, BLACK}
//...
final ChessPlayer getFirstPlayer() {
return ChessPlayer.WHITE;
}
//...
}
Methods called from constructors should be declared final.
Unlike interfaces, abstract classes can contain fields that are not static
and final and they can contain implemented methods.
26
They all have certain states (position, orientation, line color, fill color...) and
behaviors (moveTo, rotate, resize, draw...).
Some of these states and behaviors are the same for all objects: position,
fill color, moveTo.
28
http://docs.oracle.com/javase/tutorial/java/TOC.html
29
Exercise 1. Consider the Card, Deck and DisplayDeck classes that you
wrote in a previous exercise (previous lectures).
30