8

This question was asked to me in an interview. Tired of googling here I am.

I have an interface with 100 methods. I don't want to implement all those 100 methods in a single class. Is there any way I could implement these 100 methods by using more than one class and not repeating the implementation ?

For Example : Class A implements first 10 methods(only). Class B implements next 10 methods(only) and so on.

Note : 1. All the classes which implements the interface must be concrete.

As far as my knowledge on java this isn't possible. He mentioned about adapter when he asked me this question. That made me think that there's a way to do it.

Can anybody clarify me on this ?

KarthickN
  • 335
  • 1
  • 6
  • 20
  • 1
    Did you ask the version of the java ? Coz after Java8 its possible.https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html – Jabir Aug 03 '15 at 12:08
  • Use composition instead of inheritance ? (e.g. create a class that implements the interface, but delegates the actual methods down to the partial implementations). That seems to feet the requirements. – GPI Aug 03 '15 at 12:17

6 Answers6

2

Write an adapter with empty implementation of the 100 methods

 class Adapter{
    //write your empty implementations here as similar to KeyListener in Java
    // They have written a keyAdapter and make use of key adapter.

    }

ie) class Adapter implements interface1{
    public void method1(){}
    public void method2(){}
     .....
}

You can extend the adapter class in some other class and just override the methods.

class A extedns Adapter{
    public void method1(){
}
}

ie)

Shriram
  • 4,343
  • 8
  • 37
  • 64
  • He mentioned this clue about adapter. As I haven't used java 8. I couldn't catch it. Can you tell me how to do it exactly ? – KarthickN Aug 03 '15 at 12:11
  • Writing empty methods will not lead to repeating implementation of the methods? – rahimv Aug 03 '15 at 12:12
  • I think this is what he had mentioned. Thanks :) – KarthickN Aug 03 '15 at 12:38
  • 1
    this is actually what default does with less effort,but if you do not use java 8 you have to – Mustafa ASAN Aug 03 '15 at 12:39
  • @KarthickN, your original question mentions that "without repeating implementation" while this does repeat implementations. – rahimv Aug 03 '15 at 12:41
  • 1
    @rahimv It doesn't repeat all the 100 methods in every class. It does only repeat the overidden methods, which is far less and you can't go around that. – singe3 Aug 03 '15 at 12:45
  • Looks more like a hack than a solution to me (design-wise, I mean). I guess the fact that an implementation of the interface may have any-number of "unimplemented" (that is, empty body) methods should be stated upfront in the question. Plus, one can only argue that this solution avoids duplication. `method1` could very very well be needed in implementation `A` and `B`. Sure, the answer is technically valid, but I'd not actually deem it any good "in real life". – GPI Aug 03 '15 at 14:32
2

Write all the classes (A, B, C, D, E each implement 20 methods) witch extend one another without implementing the interface I:

                    I
                    |
A <- B <- C <- D <- E

And only the last one implements the interface.

Simpler exemple with only 2 methods:

public interface I {

    void a();

    void b();

}

public class A {

    public void a() {

    }

}

public class B extends A implements I {

    public void b() {

    }

}
dotvav
  • 2,808
  • 15
  • 31
  • This seems to be a good idea. I liked it. But what if the interface is not in our control ? – KarthickN Aug 03 '15 at 12:21
  • It doesn't have to be... Just implement the methods. My example above is willingly simplistic. – dotvav Aug 03 '15 at 12:22
  • 2
    This is **NOT** a proper OOP solution. – Abimaran Kugathasan Aug 03 '15 at 12:24
  • Agree with @AbimaranKugathasan, do not go that way – singe3 Aug 03 '15 at 12:25
  • 1
    While this looks a smart answer for an interview, it defeats the purpose of the interface. Interface is an enforcing contract for implementation class while your example completely bypasses that. – rahimv Aug 03 '15 at 12:28
  • Well there is no mention of OOP in the original question. If the interviewer wants to hire me with a tricky question, they will get a tricky answer. I am quite confident that the question has no correct answer in the space of proper OOP. – dotvav Aug 03 '15 at 12:32
  • Yes, Agree, there is no way I can think of to do it using only concrete classes and without repeating implementation. – rahimv Aug 03 '15 at 12:43
2

The concept you describe is called partial classes and Java does not have such a concept.

Here is a similar answer: A way to implement partial classes in java

Community
  • 1
  • 1
Johannes
  • 828
  • 12
  • 29
2

If you use Java 8 , you can define default implementations in the interface for the 100 methods like :

public interface MyInterface{

    void methodA();

    int methodB();

    default boolean methodC(String name) {
        return name.equals("Default");
    }
}

Then in your concrete classes you only implements the methods you want. All other not overriden methods will use the default implementation from the interface.

You will have to write 100 default implementations in the interface but it will save you the need to also write 100 implementations in every concrete class implementing that interface.

Again, this is only available since Java 8.

singe3
  • 2,065
  • 4
  • 30
  • 48
2

If the interface methods defined with default implementation ;

public interface I {

    default void a(){ 
   //implementation
}

      default void b(){ 
   //implementation
}

       default void c(){ 
   //implementation
}

   //97 more

}

public class A implements I{
    @override
    public void a() {

    }

}

public class B extends A  {
    @override
    public void b() {

    }

public class C extends B {
    @override
    public void c() {

    }

}

Even without inheritance classes can be independent from each other and they can provide implementation for different methods

Mustafa ASAN
  • 3,747
  • 2
  • 23
  • 34
1

You are correct - any concrete class must implement all methods, so the only way you can not do it is either extend the class that implements given interface and override some of the methods in subclass or implement methods calling implementations from other classes

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
  • This is correct in Java <= 7. However in Java >= 8 , you don't need to implement all methods if the interface provides default implementations. – singe3 Aug 03 '15 at 12:23
  • 1
    @singe3 default implementation provided by the interface **is** a concrete implementation that gets inherited by the class in exactly the same way as a concrete implementation from superclass would be – Germann Arlington Aug 03 '15 at 12:33
  • The question was how not to repeat the 100 implementations in every concrete class. This solution answers the question because you only implements once the 100 methods, then implements only the methods you need in the concrete classes. – singe3 Aug 03 '15 at 12:41
  • Full implementation can be provided in one of the classes, other implementation will need to call it. If the interface provides default implementations for **all** methods than you can implement only selected methods in each of the implementing classes, **but** it depends on how the interface is defined – Germann Arlington Aug 03 '15 at 17:19