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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
goscha01
Posts: 6
Joined: Fri Feb 07, 2020 7:16 pm
Contact:

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

Post by goscha01 »

Hi!
Is it not a problem because both m1() methods are implemented similarly?
Thanks

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Not sure what you mean. Can you be more clear?
Also, try to compile the code and see what happens.
If you like our products and services, please help us by posting your review here.

goscha01
Posts: 6
Joined: Fri Feb 07, 2020 7:16 pm
Contact:

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

Post by goscha01 »

I meant if the methods had two different implementations, would it cause a compiler error?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

goscha01
Posts: 6
Joined: Fri Feb 07, 2020 7:16 pm
Contact:

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

Post by goscha01 »

The IDE wants that I change the method to default or static.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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).
If you like our products and services, please help us by posting your review here.

noeloo
Posts: 61
Joined: Sat Feb 15, 2020 8:56 am
Contact:

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

Post by noeloo »

Which methods/fields are ambiguous here?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

m1() is in T2 as well as T3. So, T3 has potentially two m1 methods with the same signature.
If you like our products and services, please help us by posting your review here.

noeloo
Posts: 61
Joined: Sat Feb 15, 2020 8:56 am
Contact:

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

Post 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);
}
Last edited by noeloo on Fri May 22, 2020 1:34 pm, edited 1 time in total.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

noeloo
Posts: 61
Joined: Sat Feb 15, 2020 8:56 am
Contact:

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

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Put default m1 in T1 as well as T2, then see if T3 compiles.
If you like our products and services, please help us by posting your review here.

noeloo
Posts: 61
Joined: Sat Feb 15, 2020 8:56 am
Contact:

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

Post 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);
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

noeloo
Posts: 61
Joined: Sat Feb 15, 2020 8:56 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Just like the method, put a field by the same name in T1 and T2. Post the code if have an issue.
If you like our products and services, please help us by posting your review here.

gayanw
Posts: 4
Joined: Sun Jan 23, 2022 9:03 am
Contact:

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

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

gayanw
Posts: 4
Joined: Sun Jan 23, 2022 9:03 am
Contact:

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

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests