About Question enthuware.ocajp.i.v8.2.1477 :
Moderator: admin
-
- Site Admin
- Posts: 10046
- 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: 10046
- 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: 10046
- 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: 10046
- 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: 10046
- 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 67 guests