KarthickN
KarthickN

Reputation: 335

Implementing one interface using multiple classes

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 ?

Upvotes: 8

Views: 7430

Answers (6)

Mustafa ASAN
Mustafa ASAN

Reputation: 3835

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

Upvotes: 2

dotvav
dotvav

Reputation: 2848

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

    }

}

Upvotes: 2

Germann Arlington
Germann Arlington

Reputation: 3353

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

Upvotes: 1

singe3
singe3

Reputation: 2105

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.

Upvotes: 2

Shriram
Shriram

Reputation: 4421

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)

Upvotes: 2

Johannes
Johannes

Reputation: 868

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

Upvotes: 2

Related Questions