Page 1 of 1
About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Sat Jun 06, 2015 3:57 am
by FrodoBaggins
This question confused me. Doesn't a class implementing an interface have to implement its methods as well?
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Sat Jun 06, 2015 6:03 am
by admin
Not if the class is abstract!
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Mon Jun 08, 2015 3:31 am
by FrodoBaggins
You mean the Getaddress() method is abstract?
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Mon Jun 08, 2015 3:52 am
by admin
No, I was just answering your question about the requirement of a class implementing an interface.
getAddress is a "default" method in the interface. In other words, it is already implemented by the interface so a non-abstract class implementing that interface doesn't necessarily have to implement it.
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Thu Jul 30, 2015 7:28 am
by islam.amin099
Hi there,
Why is the Bungalow's getAddress method is called rather than that of House since MyHouse implements both? And shouldn't this be an ambiguous call to getAddress method?
Thanks in advance.
EDIT: Ok now I understood the issue thanks anyway.
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Thu Jul 30, 2015 10:19 pm
by admin
The implementation provided by Bungalow overrides the implementation provided by House. That is what the explanation is trying to explain. A subinterface can override a default method. So MyHouse doesn't have two versions on getAddress. It gets only one, which is the one provided by Bungalow.
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Thu Feb 25, 2016 5:21 am
by rachnagoel18
class MyHouse implements Bungalow, House{ }
this class will not compile until it provides implementation of getAddress() method. This rule is applicable since Java SE 8. I have tried same code snippet at my end.
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Thu Feb 25, 2016 10:49 am
by admin
Not sure how you are trying but the code given in the question compiles fine.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Thu May 26, 2016 10:18 am
by JaredTse
How is this question different from
postingaskquestion.php?mode=post&f=2&qi ... .v8.2.1479
Which says that this will not compile:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Thu May 26, 2016 10:23 am
by JaredTse
The code
Code: Select all
interface I1{
public default void m1() {
System.out.println("in I1.m1");
}
}
interface I2{
public default void m1(){
System.out.println("in I2.m1");
}
}
class CI implements I1, I2{//This class will not compile.
}
class C2 implements I1, I2{ //This class will compile because it provides its own implementation of m1.
public void m1(){
System.out.println("in C2.m1");
}
}
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Thu May 26, 2016 10:39 am
by admin
Both are different situations. Bungalow extends House but I2 doesn't extend I1.
-Paul.
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Thu Aug 03, 2017 4:03 am
by vidyats
In this question, is there any way to get Houses default method to be used rather than overridden method?
say I want to print "101 Main Str";
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Thu Aug 03, 2017 5:14 am
by admin
No, that is not possible from any Bungalow instance.
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Mon Oct 30, 2017 2:46 pm
by asenevtimov
What about System.out.pirntln(House.super.getAddress());
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Mon Oct 30, 2017 8:53 pm
by admin
What happened when you tried it out?
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Thu Dec 14, 2017 6:18 pm
by pinnacle28188
I thought that interfaces could only be method signatures with out any code?
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Thu Dec 14, 2017 9:52 pm
by admin
pinnacle28188 wrote:I thought that interfaces could only be method signatures with out any code?
That was indeed the case before Java 8. Java 8 allows you to define methods (static and default). Here is a link that explains this nicely:
https://www.journaldev.com/2752/java-8- ... ult-method
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Fri Dec 15, 2017 10:34 am
by pinnacle28188
Thanks for that explanation.
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Fri Jan 26, 2018 1:34 pm
by kevin35
Code: Select all
interface House {
int x=1;
public default String getAddress() {
return "101 Main Str";
}
}
interface Bungalow extends House {
int x=1;
public default String getAddress() {
return "101 Smart Str";
}
}
class MyHouse implements Bungalow, House {
void foo() {
getAddress();
System.out.println(x); //compile error: The field x is ambiguous
}
}
Hello Paul,
class MyHouse doesn't have a problem with the overriding methods
but why it does not work for variable?
doesn't Bungalow hide the variable x in house?
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Posted: Fri Jan 26, 2018 10:01 pm
by admin
Yes, x in Bungalow does hide the x in House but your MyHouse class implements both the interfaces so it gets access to two xs independently. If you just implement Bungalow, Bungalow's x will be used instead of House's x.
Unlike static fields (and also static methods and instance fields), instance methods are overridden (not hidden). There are different rules for overriding. The nearest superclass's or interface's method overrides any other implementation of the same method that it might have inherited. So in this case Bungalow's getAddress will override House's getAddress. Unlike, the variable x, MyHouse will not get two versions of getAddress.