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

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

Moderator: admin

Post Reply
admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Both are different situations. Bungalow extends House but I2 doesn't extend I1.
-Paul.
If you like our products and services, please help us by posting your review here.

vidyats
Posts: 5
Joined: Wed Jul 12, 2017 11:58 pm
Contact:

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

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

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

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

Post by admin »

No, that is not possible from any Bungalow instance.
If you like our products and services, please help us by posting your review here.

asenevtimov
Posts: 2
Joined: Mon Oct 16, 2017 5:48 am
Contact:

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

Post by asenevtimov »

What about System.out.pirntln(House.super.getAddress());

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

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

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.

pinnacle28188
Posts: 2
Joined: Thu Dec 14, 2017 6:13 pm
Contact:

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

Post by pinnacle28188 »

I thought that interfaces could only be method signatures with out any code?

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

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

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

pinnacle28188
Posts: 2
Joined: Thu Dec 14, 2017 6:13 pm
Contact:

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

Post by pinnacle28188 »

Thanks for that explanation.

kevin35
Posts: 8
Joined: Mon Dec 25, 2017 4:30 am
Contact:

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

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

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

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

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