Interfaces Can Be Extended
One interface can inherit another through use of the keyword extends. The syntax is the similar as for inheriting classes. Whenever a class implements an interface which inherits another interface, it has to provide implementations for all functions defined inside the interface inheritance chain. Subsequent is an example:
/ / One interface can extend another.
interface A {
void meth1 ( );
void meth2 ( );
}
/ / B now includes meth1 ( ) and meth2 ( ) - - it adds meth3 ( ).
interface B extends A {
void meth3 ( );
}
/ / This class has to implement all of A and B
class MyClass implements B {
public void meth1 ( ) {
System.out.println ("Implement meth1 ( )");
}
public void meth2 ( ) {
system.out.println ("Implement meth2 ( ));
}
public void meth3 ( ) {
System.out.println ("Implement meth3 ( ) .") ;
}
}
class IFExtend {
public static void main (String arg [ ]) { Myclass ob = new Myclass ( ) ;
ob.meth1( ); ob.meth2( ); ob.meth3( );
}
}
As an experiment you may need to try erasing the implementation formeth1 (in MyClass. it will cause a compile-time error. As begin previously, any class the implements an interface must implement all functions defined through that interface, involving any which are inherited from other interfaces.
While the examples we've involved in this book do not make often use for packages or interfaces, both of these tools are an important part of the Java programming environment. Virtually all real programs and applets which you write in Java will be contained inside packages. A number will possibly implement interfaces as well. It is important, thus, which you be comfortable along with their usage.