About Question enthuware.ocajp.i.v8.2.1477 :
Moderator: admin
-
- Posts: 14
- Joined: Tue Jun 02, 2015 8:32 am
- Contact:
About Question enthuware.ocajp.i.v8.2.1477 :
This question confused me. Doesn't a class implementing an interface have to implement its methods as well?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Not if the class is abstract!
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Tue Jun 02, 2015 8:32 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
You mean the Getaddress() method is abstract?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Mon Jul 20, 2015 1:47 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
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.
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.
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Thu Feb 25, 2016 12:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
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.
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.
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Not sure how you are trying but the code given in the question compiles fine.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Sat Apr 23, 2016 2:52 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
How is this question different from
postingaskquestion.php?mode=post&f=2&qi ... .v8.2.1479
Which says that this will not compile:
postingaskquestion.php?mode=post&f=2&qi ... .v8.2.1479
Which says that this will not compile:
-
- Posts: 20
- Joined: Sat Apr 23, 2016 2:52 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
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");
}
}
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Both are different situations. Bungalow extends House but I2 doesn't extend I1.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Wed Jul 12, 2017 11:58 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
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";
say I want to print "101 Main Str";
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
No, that is not possible from any Bungalow instance.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Mon Oct 16, 2017 5:48 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
What about System.out.pirntln(House.super.getAddress());
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Thu Dec 14, 2017 6:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
I thought that interfaces could only be method signatures with out any code?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
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-methodpinnacle28188 wrote:I thought that interfaces could only be method signatures with out any code?
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Thu Dec 14, 2017 6:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
Thanks for that explanation.
-
- Posts: 8
- Joined: Mon Dec 25, 2017 4:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
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
}
}
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?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1477 :
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.
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 121 guests