Implementing more than one Interfaces

6 December 2007

Interfaces contain abstract methods that implementing class has to implement. It is must for the implementing class to implement all the methods found in the interface. Sometimes you face a situation where you have to implement more than one interface. How to do that?

One way is to simply try to write one interface with all the abstract methods. This is not at all flexible as all of these interfaces might be used in different scenarios. So its better to keep each as a separate interface.

Another option is that interface extend each other. This is also not flexible. Review the example below:

public interface InterfaceA { 
	public void interfaceAMethod1(); 
	public void interfaceAMethod2(); 
}
 
public interface InterfaceB extends InterfaceA{
	public void interfaceBMethod1();
	public void interfaceBMethod2();
}
 
public interface InterfaceC extends InterfaceB {
	public void interfaceCMethod1();
	public void interfaceCMethod2();
}
 
public class MainClass implements InterfaceC {
 
	public void interfaceAMethod1() {
		// TODO Auto-generated method stub
 
	}
 
	public void interfaceAMethod2() {
		// TODO Auto-generated method stub
 
	}
 
	public void interfaceBMethod1() {
		// TODO Auto-generated method stub
 
	}
 
	public void interfaceBMethod2() {
		// TODO Auto-generated method stub
 
	}
 
	public void interfaceCMethod1() {
		// TODO Auto-generated method stub
 
	}
 
	public void interfaceCMethod2() {
		// TODO Auto-generated method stub
 
	}
}

The right way is to implement all the interfaces in the required class and then provided the implementation of all the abstract methods. In Java, you cannot extend more than once class but you can implement more than one class. Review the example below:

public interface InterfaceA { 
	public void interfaceAMethod1(); 
	public void interfaceAMethod2(); 
}
 
public interface InterfaceB{
	public void interfaceBMethod1();
	public void interfaceBMethod2();
}
 
public interface InterfaceC {
	public void interfaceCMethod1();
	public void interfaceCMethod2();
}
 
public class MainClass implements InterfaceA,InterfaceB,InterfaceC {
 
	public void interfaceAMethod1() {
		// TODO Auto-generated method stub
 
	}
 
	public void interfaceAMethod2() {
		// TODO Auto-generated method stub
 
	}
 
	public void interfaceBMethod1() {
		// TODO Auto-generated method stub
 
	}
 
	public void interfaceBMethod2() {
		// TODO Auto-generated method stub
 
	}
 
	public void interfaceCMethod1() {
		// TODO Auto-generated method stub
 
	}
 
	public void interfaceCMethod2() {
		// TODO Auto-generated method stub
 
	}
}

del.icio.us:Implementing more than one Interfaces  digg:Implementing more than one Interfaces  spurl:Implementing more than one Interfaces  wists:Implementing more than one Interfaces  simpy:Implementing more than one Interfaces  newsvine:Implementing more than one Interfaces  blinklist:Implementing more than one Interfaces  furl:Implementing more than one Interfaces  reddit:Implementing more than one Interfaces  fark:Implementing more than one Interfaces  blogmarks:Implementing more than one Interfaces  Y!:Implementing more than one Interfaces  smarking:Implementing more than one Interfaces  magnolia:Implementing more than one Interfaces  segnalo:Implementing more than one Interfaces  gifttagging:Implementing more than one Interfaces

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: Implementing more than one Interfaces

One Response to “Implementing more than one Interfaces”

  1. Rickard Öberg Says:

    Or you can just use Qi4j where you can let one object implement any number of interfaces, and have it be implemented by any number of mixins(mixin=Java class), each of which can implement as little as one method, or as many methods in as many interfaces as you want. Any combination is fine, as long as in the end all methods are implemented somehow.

    Or don’t implement any methods at all, if they are just getters and setter, or similar, and use standard implementations for all of them.

    This is basically the composition pattern, but put into a framework so that it’s easy to use.

Leave a Reply