About Question enthuware.ocajp.i.v8.2.900 :
Moderator: admin
-
- Posts: 6
- Joined: Fri Feb 07, 2020 7:16 pm
- Contact:
About Question enthuware.ocajp.i.v8.2.900 :
Hi!
Is it not a problem because both m1() methods are implemented similarly?
Thanks
Is it not a problem because both m1() methods are implemented similarly?
Thanks
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
Not sure what you mean. Can you be more clear?
Also, try to compile the code and see what happens.
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.
-
- Posts: 6
- Joined: Fri Feb 07, 2020 7:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
I meant if the methods had two different implementations, would it cause a compiler error?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Fri Feb 07, 2020 7:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
The IDE wants that I change the method to default or static.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
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.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
Which methods/fields are ambiguous here?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
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.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
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.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
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.
In your code, no such thing is happening.
If you like our products and services, please help us by posting your review here.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
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
This simply prints "T3".
This sentence
suggests that there is some problem is case of default methods (if I understand it correctly). So what is the problem here?Having ambiguous fields or methods does not cause any problem by itself (except in the case of default methods)
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);
}
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
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.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
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);
}
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
The following code (interface T3) doesn't compile:
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.
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 {
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
OK, thank you. But I have a couple more questions to the explanation of this question. Could you give an example of ambiguous field?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
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.
-
- Posts: 4
- Joined: Sun Jan 23, 2022 9:03 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
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.
Isn't that the case? I still can't seem to understand whether there's any ambiguity or not.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
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.
-
- Posts: 4
- Joined: Sun Jan 23, 2022 9:03 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
In the question T1 does not have any methods though.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.900 :
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.
I thought you were talking about the discussion above.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 138 guests