Interface extending Interface
4 December 2007An interface can extend other interface but cannot implement any interface. This makes sense because interface cannot have any implementation. An interface can only contain abstract methods that are implemented by the class implementing that interface.
Lets do this with an example.
Create a package named myinterfaces. Using conventions, package name should be in small case. Now create an interface named InterfaceA with 2 methods.
package myinterfaces; public interface InterfaceA { public void interfaceAMethod1(); public void interfaceAMethod2(); }
We will now create another interface named interfaceB that will extend from interfaceA.
package myinterfaces; public interface InterfaceB extends InterfaceA { public void interfaceBMethod1(); public void interfaceBMethod2(); }
InterfaceB has two methods defined and it extends from InterfaceA.
Now lets come to the interesting point. We now will create a class that will implement interface InterfaceB.
package myinterfaces; public class MainClass implements InterfaceB { public void interfaceBMethod1() { } public void interfaceBMethod2() { } }
In MainClass, we implemented methods defined in interfaceB but still compiler shows the following error message:
The type MainClass must implement the inherited abstract method InterfaceA.interfaceAMethod1() The type MainClass must implement the inherited abstract method InterfaceA.interfaceAMethod2()
InterfaceB extends from InterfaceA, so MainClass which is implementing interface InterfaceB has to implement abstract methods of both interfaces.
Easy way in Eclipse is to right click the class name > Source > Override/Implement Methods
You will see the following:
You have to implement all the abstract methods.
Related Posts:
- An interface extending an interface(II)
- An interface extending an interface(I)
- Creating a Thread (implementing Java Runnable Interface)
- The Thread Class - I
- Behavioral Pattern - Iterator Pattern (Example) - 1
- Threads In Java
- Generating Interface from a Class
- Using interfaces in APIs - I
- Using interfaces in APIs - II
- Extract Interface (I)
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Interface extending Interface