views:

64

answers:

5

Hello all, I have a doubt in this scenario, I have posted some example code here....

public interface a{

      public void m1();
      public void m2();
      public void m3();
         .
         .
         .
      public void m100();
}


public class A implements a{

       public void m3(){

           // implementation code     

           }

 // Here i need to implement only m3() method but not all 100 methods
 //basically i have to implement all the other 99 methods
 // but here i don't want to either implement or provide skeleton declarations for all 
 //remaining.... what should i do for compiling this ????


}

Could anyone help this?

+1  A: 

I would suggest making a single abstract class called AbstractA that implemented the interface A but had null implementations for all of its methods. Then have each of your implementing classes extend AbstractA.

Also I would wonder why you have a single interface with all these methods when they aren't intended all to be implemented by each class that implements the interface. You've almost certainly got a core design problem here.

Jim Kiley
That's exactly what MouseAdapter, WindowAdapter, etc. do for the corresponding MouseListener, WindowListener interfaces in Swing.
Skeptic
i have faced this question in an interview....
Renuka
@Renuka The interviewer wanted to know, if you understood the diff b/w an Interface and an Abstract class and how can you use them together.
zengr
@Skeptic - Just because that's what they did around 12-15 yrs ago (Swing largely inherited this approach) doesn't mean it's good design practice today. You could also argue that given the nature of those two beasts(listeners to UI events), they are the exception that proves the rule.
Jim Leonardo
A: 

You can meta code, write a program to write up 99 methods that throw a NotImplementedException.

Or, you can make "a" a class instead of an interface (perhaps abstract) and extend it, implementing only what you need. The disadvantage is that your class cannot extend any other classes if you do so as Java does not support multiple inheritance.

Adam
A: 

a common approach to this type of problem would be to create base class that implements the interface and provides STUB implementations for all the methods.

you can see this used in Swing for example the AbstractAction class or the MouseAdapter class are trivial implementation of the Action interface or the MouseListener interface designed to make it easier to implement. To do this in your case you could provide a class AbstractA

public abstract class AbstractA implmements a
{
    public void m1() {}
    public void m2() {}
    ///... and so on
}

then you would create your actual class

public class A extends AbstractA
{
    public void m3()
    {
        //your implmentation
    }
}
luke
A: 

Unless you implement all 100 methods in class A or in some superclass of A, or at least stub them out as abstract methods, A does NOT implement the interface a.

If you really want to separate the logic of each method out, so that you don't have to implement 100 methods in one class (which would be ugly), consider the strategy pattern.

M is an an interface that defines one method:

public interface M {
    public void go();
}

Each implementation of M implements the logic of one of the m methods:

public class M1 implements M {
    public void go() {
        //logic for m1
    }
}

public class M2 implements M {
    public void go() {
        //logic for m2
    }
}

and so on.

Now, your class A refers to the 100 M objects, and each method simply calls go() on one of the objects. You can implement and test the logic of each separately.

public class A implement a {
    M[] ms = {
            new M1(),
            new M2(),
            new M3(),
            ... ,
            new M100()};

    private do(int index) {
        ms[index].go();
    }

    public m1 {
        do(0);
    }
    public m2 {
        do(1);
    }

    public m3 {
        do(2);
    }

   ... 

    public m100 {
        do(99);
    }
}
Matthew Flynn
+1  A: 

You could use a Proxy.

MyInterface instance = Proxy.newProxyInstance(MyInterface.class.getClassLoader(), MyInterface.class, new InvocationHandler(){
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
        if(!method.getName().equals("methodICareAbout")) {
           throw new UnsupportedOperationException();
        }

        //Implement your method here

     }
  });

Disclaimer: I would never recommend actually using a solution like this on one of my projects, but it does allow you to implement a single method without dealing with all the others.

IMO having an interface with 100 methods on it of which you only want to implement one is a major code smell that indicates you should probably rethink your design. But if your hands are tied and you have no choice but to use the existing interface this solution would work.

Mike Deck