Let’s have a look at this code:
package rpg;
interface A {
// An empty interface
}
public class WarriorClass extends CharacterClass implements A {
final int bab;
public WarriorClass(int a) {
bab = a;
}
public static void main(String[] args) {
A a = new WarriorClass(1);
System.out.println(((WarriorClass) a).bab);
}
}
First question: Why is it possible to use the interface A as the type of a?
Second question: Since A does not know about the iVar bab why does
System.out.println(((WarriorClass) a).bab);
print the correct value 1?
You do not store the class “in” the interface. You are storing a reference to the class in a variable of the interfice class.
Java only works with references. All have the same length (like a pointer in C). The references are typed so you must assign to it an object of an appropiate class.
As for the relationship, extending a class or implementing an interface means that the new class objects belong also to the original class. So all objects are instances of
Object. Since by extending/implementing you affirm that the new class follows the contract (specification) of the superclass/interface, they are do belong to it.