December 2004
Intermediate to advanced
608 pages
11h 47m
English
It is well known that a class can implement any number of interfaces at compile time by specifying the list of interface names and providing implementations of the required methods. Any class that implements an interface may be cast to the interface type at run time. A typical, if simple, example is shown in Listing A.1.
Example A.1. A simple interface and its use
public Interface MyInterface {
public void sayHello();
}
pubilc class MyClass implements MyInterface {
public void sayHello() {
System.out.println("Hello, world");
}
}
public class MyDemo {
public static void main(String argv[]) {
MyClass c = new MyClass();
MyInterface i = (MyInterface) c;
i.sayHello();
}
}
In this example the cast to MyInterface ...