Interface extending Interface

4 December 2007

An 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:
Impleminting abstract methods
You have to implement all the abstract methods.

del.icio.us:Interface extending Interface  digg:Interface extending Interface  spurl:Interface extending Interface  wists:Interface extending Interface  simpy:Interface extending Interface  newsvine:Interface extending Interface  blinklist:Interface extending Interface  furl:Interface extending Interface  reddit:Interface extending Interface  fark:Interface extending Interface  blogmarks:Interface extending Interface  Y!:Interface extending Interface  smarking:Interface extending Interface  magnolia:Interface extending Interface  segnalo:Interface extending Interface  gifttagging:Interface extending Interface

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

Leave a Reply