Page 1 of 1

About Question enthuware.ocajp.i.v8.2.900 :

Posted: Sat Feb 08, 2020 3:23 pm
by goscha01
Hi!
Is it not a problem because both m1() methods are implemented similarly?
Thanks

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Sun Feb 09, 2020 12:52 am
by admin
Not sure what you mean. Can you be more clear?
Also, try to compile the code and see what happens.

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Wed Feb 12, 2020 1:42 pm
by goscha01
I meant if the methods had two different implementations, would it cause a compiler error?

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Wed Feb 12, 2020 10:11 pm
by admin
What happened when you tried it out?

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Thu Feb 13, 2020 11:21 am
by goscha01
The IDE wants that I change the method to default or static.

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Thu Feb 13, 2020 11:33 am
by admin
Please post the complete and exact code that you tried and the message generate by javac on command line (and, of course, your doubt).

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Wed May 20, 2020 10:20 am
by noeloo
Which methods/fields are ambiguous here?

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Wed May 20, 2020 10:42 am
by admin
m1() is in T2 as well as T3. So, T3 has potentially two m1 methods with the same signature.

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Thu May 21, 2020 12:37 pm
by noeloo
OK, but when I added default keyword to them, it also does not seem to cause a problem. So why is this exception there, in description?

Code: Select all

interface T1 {
}

interface T2 {
    int value = 10;
    default void m1() {};
}

interface T3 extends T1, T2 {
    default public void m1() {};
    public void m1(int x);
}

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Thu May 21, 2020 10:56 pm
by admin
In the code given in the question, T3 extends T1 and T2. So, T3 has two methods with the same signature. What happens when a class implements T3? Will it be implementing T1's m1 or T3's? What if both interfaces have m1 as default methods?

In your code, no such thing is happening.

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Fri May 22, 2020 1:39 pm
by noeloo
Wow, how stupid. I forgot to extend T1 and T2. I edited code in my comment - but it does not change anything. I was able to have a class implementing T3 and create an instance. When both methods are default, I see the one from T3 is called.

This sentence
Having ambiguous fields or methods does not cause any problem by itself (except in the case of default methods)
suggests that there is some problem is case of default methods (if I understand it correctly). So what is the problem here?

Code: Select all

public class Test {
    public static void main(String[] args) {
        var t = new InterfaceTest();
        t.m1();
    }
}

class InterfaceTest implements T3 {
    public void m1(int x) {
    }
}

interface T1 {
}

interface T2 {
    int value = 10;
    default void m1() {
        System.out.println("T2");
    };
}

interface T3 extends T1, T2 {
    default public void m1() {
        System.out.println("T3");
    };
    public void m1(int x);
}
This simply prints "T3".

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Fri May 22, 2020 2:23 pm
by admin
Put default m1 in T1 as well as T2, then see if T3 compiles.

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Sat May 23, 2020 7:04 am
by noeloo
Yes, it does...

Code: Select all

package classes;

public class Test {
    public static void main(String[] args) {
        var t = new InterfaceTest();
        t.m1();
    }
}

class InterfaceTest implements T3 {
    public void m1(int x) {
    }
}

interface T1 {
    default void m1() {
        System.out.println("T1");
    }
}

interface T2 {
    int value = 10;
    default void m1() {
        System.out.println("T2");
    }
}

interface T3 extends T1, T2 {
    default public void m1() {
        System.out.println("T3");
    }
    public void m1(int x);
}

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Sat May 23, 2020 8:19 am
by admin
The following code (interface T3) doesn't compile:

Code: Select all

interface T1 {
    default void m1() {
        System.out.println("T2");
    };

}

interface T2 {
    default void m1() {
        System.out.println("T2");
    };
}

interface T3 extends T1, T2 {
}
This illustrates the issue with default methods. If m1 were just a regular interface method, T3 would have compiled. But since m1 is a default method, T3 doesn't compile. You need to resolve the ambiguity by providing an implementation of m1 in T3 to make T3 compile.

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Sat May 30, 2020 1:26 pm
by noeloo
OK, thank you. But I have a couple more questions to the explanation of this question. Could you give an example of ambiguous field?

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Sun May 31, 2020 1:13 am
by admin
Just like the method, put a field by the same name in T1 and T2. Post the code if have an issue.

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Sat Feb 05, 2022 2:17 am
by gayanw
T3#m1 seems to override T2#m1. So when a class implements T3 it only need to implement m1 that is declared in T3.

Isn't that the case? I still can't seem to understand whether there's any ambiguity or not.

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Sat Feb 05, 2022 3:42 am
by admin
But T3 extends T1 and T2 and both of them have m1. So, when a class implements T3, which m1 will be invoked? That is the ambiguity because there are two equally valid options available. Please go through the complete discussion above carefully.

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Sun Feb 06, 2022 1:49 am
by gayanw
admin wrote:
Sat Feb 05, 2022 3:42 am
But T3 extends T1 and T2 and both of them have m1. So, when a class implements T3, which m1 will be invoked? That is the ambiguity because there are two equally valid options available. Please go through the complete discussion above carefully.
In the question T1 does not have any methods though.

Re: About Question enthuware.ocajp.i.v8.2.900 :

Posted: Sun Feb 06, 2022 2:26 am
by admin
Right, that's why the correct option is "there is nothing wrong with the code". There is no ambiguity there.

I thought you were talking about the discussion above.