0

Recently i have faced it in interview. we have two interfaces

interface I1{
    void m1();
    void m2();
    void method();
}

interface I2{
    void m3();
    void m4();
    void method();
}

class A implements I1,I2{
    void m1(){}
    void m2(){}
    void m3(){}
    void m4(){}

    void method(){}
}

so here comes the requirement as we need implementing in such a way that interface I1 method() to print "Test" and interface I2 method() to print "Oops" on execution. but class A can implementation can have only one method() in it.(ref) Do we have any possibility of achieving this.

Community
  • 1
  • 1
snofty
  • 70
  • 7
  • Are you allowed to use default methods? In which case, I would use default implementation in each of the interfaces to achieve what you're asking. See [this](https://blog.idrsolutions.com/2015/01/java-8-default-methods-explained-5-minutes/) – Dana Dec 22 '16 at 03:19
  • no and it is with jdk 1.1 version, can we do it? – snofty Dec 22 '16 at 03:23
  • Possible duplicate of [Implementing two interfaces in a class with same method. Which interface method is overridden?](http://stackoverflow.com/questions/2801878/implementing-two-interfaces-in-a-class-with-same-method-which-interface-method) –  Dec 22 '16 at 03:27
  • No, there's no way. The compiler handles those two Interface-methods as one in `A`. There's absolutely no way of distinguishing them. –  Dec 22 '16 at 03:29
  • its different, we need the content of the method to be different for both the interfaces, as i already provided ref in my question. – snofty Dec 22 '16 at 03:31
  • 2
    I don't think there's any nice way to do this in older versions of Java. See similar question [here](http://stackoverflow.com/questions/2598009/method-name-collision-in-interface-implementation-java) – Dana Dec 22 '16 at 03:35
  • Are you sure this was java interview question. C++ can do this. I think that C# also can, but in Java - NO WAY. – Aleydin Karaimin Dec 22 '16 at 05:56
  • yes, question is on java inheritance test. – snofty Dec 22 '16 at 06:48

2 Answers2

1

You can give a default implementation for a method in an interface:

public interface I1 {
    default void method() {
        System.out.println("Test");
    }
}

public interface I2 {
    default void method() {
        System.out.println("Oops");
    }
}

When implementing both interfaces, you can choose which default implementation you want to invoke with the following syntax

public class A implements I1, I2 {
    @Override
    public void method() {
        I1.super.method(); //or I2.super.method()
    }
}

If you need to print "Test Oops" you can also call the two default implementation (and choose the order)

public class A implements I1, I2 {
    @Override
    public void method() {
        I1.super.method();
        I2.super.method();
    }
}

Edit

Before Java 8 you can achieve something similar but it is far more verbose. (note that I am using some kind of decorator here)

public interface I1 {
    void m1();
    void m2();
    void method();
}

public interface I2 {
    void m3();
    void m4();
    void method();
}

public final class DefaultI1 implements I1 {
    @Override
    public void m1() {
        //TODO
    }

    @Override
    public void m2() {
        //TODO
    }

    @Override
    public void method() {
        System.out.println("Test");
    }
}

public final class DefaultI2 implements I2 {
    @Override
    public void m3() {
        //TODO
    }

    @Override
    public void m4() {
        //TODO
    }

    @Override
    public void method() {
        System.out.println("Test");
    }
}

public final class A implements I1, I2 {
    private final I1 i1;
    private final I2 i2;

    public A(I1 i1, I2 i2) {
        this.i1 = i1;
        this.i2 = i2;
    }

    @Override
    public void m1() {
        i1.m1();
    }

    @Override
    public void m2() {
        i1.m2();
    }

    @Override
    public void m3() {
        i2.m3();
    }

    @Override
    public void m4() {
        i2.m4();
    }

    @Override
    public void method() {
        i1.method();
        i2.method();
    }
}

Usage

public static void main(String[] args) {
    A a = new A(new DefaultI1(), new DefaultI2());
    a.method(); //prints "Test Oops"
}
Spotted
  • 4,021
  • 17
  • 33
  • thxs for input, interface with default methods introduces in latest jdk, can we achieve it with older versions.? – snofty Dec 22 '16 at 06:53
  • this sound good, As we can't write two identical methods in same class in java.. but what about `m1(), m2()..` methods? then in `class A`, we can implement `m1() {i1.m1();}` and `DefualtI1` class should implement `m1()` method. – snofty Dec 22 '16 at 07:16
  • @snofty Edited my edit with `m1()`, `m2()`, `m3()` and `m4()` – Spotted Dec 22 '16 at 07:22
-2
interface I1{
public void m();
}
interface I2{
public  void m();
}

public class Test{

public static void main(String[] args){
    I1 i1 = new I1(){
        public void m(){
            System.out.println("W");
        }
    };
    I2 i2 = new I2(){
        public void m(){
            System.out.println("M");
        }
    };
    i1.m();
    i2.m();
}


}
wylasr
  • 117
  • 9
  • i think your question do not clear.why interface can have a method body to print something.can u explain. – wylasr Dec 22 '16 at 05:50
  • it is a question asked to validate inheritance principles of java, whether this can achieved or not, and printing was just choice of identify the output. – snofty Dec 22 '16 at 06:51