Page 1 of 1

About Question enthuware.ocpjp.v7.2.1726 :

Posted: Wed Dec 23, 2015 10:35 am
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.

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

Posted: Wed Dec 23, 2015 11:40 am
by admin
Why do you think so?

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

Posted: Mon Mar 14, 2016 11:40 am
by sumanenthu
I understood your point? But why can't it return "thegoodpart"?

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

Posted: Mon Mar 14, 2016 1:52 pm
by admin
It does print "thegoodpart".

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

Posted: Tue May 17, 2016 8:04 pm
by TejWidJava
After removing getData() in Inner Layer classs why its giving StackOverflowError?

Its very strange.
:roll: :?:

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

Posted: Tue May 17, 2016 10:13 pm
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.

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

Posted: Sat Jan 14, 2017 6:25 am
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!

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

Posted: Sun Jan 15, 2017 12:08 am
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.

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

Posted: Sun Jan 15, 2017 12:45 am
by jagoneye
That was my first guess! Thanks!

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

Posted: Tue Jul 04, 2017 1:47 pm
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());
}
}

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

Posted: Tue Jul 04, 2017 11:00 pm
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
}

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

Posted: Wed Jul 05, 2017 5:45 pm
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!