About Question enthuware.ocpjp.v7.2.1726 :

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

Moderator: admin

Post Reply
PtFyEH
Posts: 8
Joined: Sun Dec 20, 2015 6:28 pm
Contact:

About Question enthuware.ocpjp.v7.2.1726 :

Post by PtFyEH »

To access Onion's data inside Layer, you can do Onion.this.data.
Explanation could be extended to:
To access Onion's data inside Layer, you can do Onion.this.data or super.data.

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

Re: About Question enthuware.ocpjp.v7.2.1726 :

Post by admin »

Why do you think so?
If you like our products and services, please help us by posting your review here.

sumanenthu
Posts: 23
Joined: Sun Feb 21, 2016 10:12 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1726 :

Post by sumanenthu »

I understood your point? But why can't it return "thegoodpart"?

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

Re: About Question enthuware.ocpjp.v7.2.1726 :

Post by admin »

It does print "thegoodpart".
If you like our products and services, please help us by posting your review here.

TejWidJava
Posts: 2
Joined: Sat Sep 07, 2013 6:16 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1726 :

Post by TejWidJava »

After removing getData() in Inner Layer classs why its giving StackOverflowError?

Its very strange.
:roll: :?:

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

Re: About Question enthuware.ocpjp.v7.2.1726 :

Post by admin »

If Layer's getData is not present, Onion's getData() will be invoked, which calls return new Layer().getData(); This is basically an unending recursive call. Hence the error.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1726 :

Post by jagoneye »

admin wrote:If Layer's getData is not present, Onion's getData() will be invoked, which calls return new Layer().getData(); This is basically an unending recursive call. Hence the error.
What is the reasoning behind this method call? How can the innerclass access outer class's instance method? Accessing outer class variables is fine! But this is something out of the worlD! :roll: And that too without returning an actual String it still compiles!

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

Re: About Question enthuware.ocpjp.v7.2.1726 :

Post by admin »

jagoneye wrote:
admin wrote:If Layer's getData is not present, Onion's getData() will be invoked, which calls return new Layer().getData(); This is basically an unending recursive call. Hence the error.
What is the reasoning behind this method call? How can the innerclass access outer class's instance method? Accessing outer class variables is fine! But this is something out of the worlD! :roll: And that too without returning an actual String it still compiles!
Why do you think accessing outer class variables is different from accessing outer class methods? Both are members. Access rules should apply equally to both.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1726 :

Post by jagoneye »

That was my first guess! Thanks!

lucastomasini
Posts: 3
Joined: Tue Jun 20, 2017 3:10 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1726 :

Post by lucastomasini »

admin wrote:
jagoneye wrote:
admin wrote:If Layer's getData is not present, Onion's getData() will be invoked, which calls return new Layer().getData(); This is basically an unending recursive call. Hence the error.
What is the reasoning behind this method call? How can the innerclass access outer class's instance method? Accessing outer class variables is fine! But this is something out of the worlD! :roll: And that too without returning an actual String it still compiles!
Why do you think accessing outer class variables is different from accessing outer class methods? Both are members. Access rules should apply equally to both.
Layer class can access getData() method of Onion class because Layer is extending Onion, thus it inherits getData(), but not because it is an inner class of Onion. Actually, if you remove getData() from Layer and the "extends Onion" from Layer class definition, the code will not compile.

class Onion {
private String data = "skin";
private class Layer {
String data = "thegoodpart";
}
public String getData() {
return new Layer().getData(); // "Cannot resolve method getData()"
}
public static void main(String[] args) {
Onion o = new Onion();
System.out.println(o.getData());
}
}

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

Re: About Question enthuware.ocpjp.v7.2.1726 :

Post by admin »

>>new Layer().getData(); // "Cannot resolve method getData()"
This will indeed not compile because Layer's doesn't have getData. Onion Does. Layer can access Onion's getData. So the following compiles even if you remove extends Onion and getData method from Layer -

public void getX() {
System.out.println(getData()); //uses Onion's getData
}
If you like our products and services, please help us by posting your review here.

lucastomasini
Posts: 3
Joined: Tue Jun 20, 2017 3:10 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1726 :

Post by lucastomasini »

admin wrote:>>new Layer().getData(); // "Cannot resolve method getData()"
This will indeed not compile because Layer's doesn't have getData. Onion Does. Layer can access Onion's getData. So the following compiles even if you remove extends Onion and getData method from Layer -

public void getX() {
System.out.println(getData()); //uses Onion's getData
}

You are absolutely right, I missed that.

Thanks!

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests