Question: On slide 21, "Overriding", is it possible to call the
superclass's updateBBox method directly? More generally, is it
possible to call a superclass's implementation of a method from a reference
to an instance of the subclass?
Answer: Not from outside the class. For example:
Circle2 c2 = new Circle2(); c2.updateBBox(12, 6); BBox1 b = c2; b.updateBBox(12, 6);Both these calls access Circle2's implementation of updateBBox. It is not possible to make a call on c2 or b that forces it to use the superclass's implementation. However, it is possible to call the superclass's updateBBox from inside the subclass using the syntax super.updateBBox(). Note that if the subclass does not override updateBBox, the superclass implementation is called automatically.
Question: What's the difference between subclassing and subtyping?
Answer: Subclassing is a Java notion that describes how a class
inherits method signatures and implementations from other classes.
Subtyping is a formal notion that defines when an instance of one type can
be used where another type is expected. Section 7.9 of the book describes
this difference in detail (according to the book's terminology, subclassing
satisfies only the signature rule, whereas subtyping requires that
all three rules be satisfied).
As an example, consider the following classes:
class Automobile {
/**
modifies: this
effects: starts this car
throws OutOfFuelException if car has no gas
*/
public void start() { ... }
}
class Yugo extends Automobile {
/**
modifies: this
effects: car explodes
throws OutOfFuelException if car has no gas
*/
public void start() { ... }
}
In Java, Yugo is a subclass of Automobile because
it extends it, thus inheriting Automobile's method signatures and
implementation (unless overridden). However, Yugo.start() does not
satisfy the spec of Automobile.start(). This means that
Yugo is not a subtype of Automobile, since a
Yugo cannot be used when an Automobile is expected.
Question: How is a "stronger" representation invariant defined?
Answer: This will be covered in next week's lectures. For details,
see Section 7.9.2 about the properties rule of subtyping.