1

HI I have a question If interface has got four methods,and I like to implement only two methods, how this could be achieved?

Can any explain is that possible or should I go for implementing all the methods.

1
  • 4
    based on your question titles you appear to be a very confused person ;) Commented Feb 19, 2010 at 7:45

7 Answers 7

4

You can't "partially" implement an interface without declaring the implementing class abstract, thereby requiring that some subclass provide the remaining implementation. The reason for this is that an interface is a contract, and implementing it declares "I provide the behavior specified by the interface". Some other code is going to use your class via the declared interface and will expect the methods to be there.

If you know the use case does not use the other two methods you can implement them by throwing OperationNotSupported. Whether this is valid or not very much depends on the interface and the user. If the interface can legitimately be partially implemented this way it would be a code smell that the interface is poorly designed and perhaps should have been two interfaces.

You may also be able "implement" the interface by doing nothing, though this is usually only proper for "listener" or "callback" implementations.

In short, it all depends.

Sign up to request clarification or add additional context in comments.

Comments

2

If you control the design of the interface, simply split it in two. One interface specifies the two only some implementations implement, and one interface specifies the other two (or inherits the first two and adds more, your choice)

Comments

2

You can make the implementing class abstract and implement two of the 4 methods from the interface.

Comments

2

I think @sateesh 's answer is the one closer to solving your problem. Let me elaborate on it. First of all, it is imperative that any class implementing an interface should provide definitions for all of its methods. But in some cases the user may find no use for a majority of the methods in the interface save for one or two. Consider the following interface having 6 abstract methods:

 public interface HugeInterface { void a(); void b(); void c(); void d(); void e(); void f(); } 

Suppose your code finds use for the method 'c()' only and you wish to provide implementation details for only the method 'c()'. You can create a new class HugeInterfaceAdapter in a separate file which implements all the methods of the interface HugeInterface like shown below:

 public class HugeInterfaceAdapter implements HugeInterface { public void a() {} public void b() {} public void c() {} public void d() {} public void e() {} public void f() {} } 

Note that you need not provide any actual implementation code for any of the methods. Now comes the interesting part. Yes, your class in which the need to implement a huge interface arose in the first place.

public class MyClass { HugeInterfaceAdapter mySmallInterface = new HugeInterfaceAdapter() { @Override public void c() { //Your class-specific interface implementation code here. } }; } 

Now you can use the reference variable mySmallInterface in all the places where a HugeInterface is expected. This may seem a little hackish but I may say that it is endorsed officially by Java and classes like MouseAdapter bears testimony to this fact.

Comments

1

It's not possible.

You can implement all four methods, but the two you don't need should throw an UnsupportedOperationException.

1 Comment

...which raises the question of whether the class he's implemented really implements the interface.
0

If you want a concrete class which is implementing this interface, then it is not possible to have unimplemented methods, but if you make have abstract class implementing this interface then you can leave any number of methods as you want to be unimplemented.

Comments

0

As other answers mention you cannot have a concrete class implementing only some of the methods of the interface it implements. If you have no control over the interface your class is extending, you can think of having Adapter classes. The abstract Adapter class can provide dummy implementation for the methods of an interface and the client classes can extend the Adapter class. (Of course the disadvantage is that you cannot extend more than one class)

This is common practice with GUI programming (using Swing) where the event listener class might not be interested in implementing all methods specified by the EventListener interface. For example take a look at the java.awt.event.MouseListener interface and and the corresponding adapter class java.awt.event.MouseAdapter.

1 Comment

Hi, If the "event listener class might not be interested in implementing all methods specified by the EventListener interface", why won't just throw exception for those methods ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.