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 ?

2
  • 1
    Did you ask the version of the java ? Coz after Java8 its possible.docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html Commented Aug 3, 2015 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. Commented Aug 3, 2015 at 12:17

6 Answers 6

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)

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

7 Comments

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 ?
Writing empty methods will not lead to repeating implementation of the methods?
I think this is what he had mentioned. Thanks :)
this is actually what default does with less effort,but if you do not use java 8 you have to
@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.
|
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

Comments

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.

Comments

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() {

    }

}

7 Comments

This seems to be a good idea. I liked it. But what if the interface is not in our control ?
It doesn't have to be... Just implement the methods. My example above is willingly simplistic.
This is NOT a proper OOP solution.
Agree with @AbimaranKugathasan, do not go that way
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.
|
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

3 Comments

This repeats the implementation.
leave default methods body empty to force one class to implement all ,this is just a trick
This method is okay. Thank you :)
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

4 Comments

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 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
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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.