Java Reference
In-Depth Information
default double getRate() {
return Player.super.getRate();
}
}
The Superinterface-Subinterface Relationship
Interface inheritance establishes a superinterface-subinterface (also called supertype-subtype) relationship. When the
interface CharitySinger inherits from the Singer interface, the Singer interface is known as the superinterface of
the CharitySinger interface, and the CharitySinger interface is known as the subinterface of the Singer interface.
An interface can have multiple superinterfaces and an interface can be a subinterface for multiple interfaces. A
reference of a subinterface can be assigned to a variable of the superinterface. Consider the following snippet of code
to demonstrate the use of superinterface-subinterface relationship. Comments in the code explain why an assignment
will succeed or fail.
public interface Shape {
// Code goes here
}
public interface Line extends Shape {
// Code goes here
}
public interface Circle extends Shape {
// Code goes here
}
The following is sample code that you can write using these interfaces with comments explaining what the code
is supposed to do:
Shape shape = get an object reference of a Shape;
Line line = get an object reference of a Line;
Circle circle = get an object reference of a Circle;
/* More code goes here... */
shape = line; // Always fine. A Line is always a Shape.
shape = circle; // Always fine. A Circle is always a Shape.
// A compile-time error. A Shape is not always a Line. A Shape may be a Circle.
// Must use a cast to compile.
line = shape;
// Ok with the compiler. The shape variable must refer to a Line at runtime.
// Otherwise, the runtime will throw a ClassCastException.
line =(Line)shape;
 
Search WWH ::




Custom Search