Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
Mohamed Sanaulla wrote:For a) The object in the context is B and not A, hence "this" refers to the instance of B class.
For b) As the name is private in A, its not visible in class B though B extends A. Private members are only visible within the class they are declared. The constructor in B sets the value for B's name and not A's name.
Try declaring the name variable in class A to be protected and remove the name declaration from class B.
Song Guo wrote:
Mohamed Sanaulla wrote:For a) The object in the context is B and not A, hence "this" refers to the instance of B class.
For b) As the name is private in A, its not visible in class B though B extends A. Private members are only visible within the class they are declared. The constructor in B sets the value for B's name and not A's name.
Try declaring the name variable in class A to be protected and remove the name declaration from class B.
In the context above, 'this' is the reference of object B, and in B's constructor it has name = n statement. So why b.getName() gets A's name null , not b's name 'Jim' ?'
Thank you !
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
Song Guo wrote:In the context above, 'this' is the reference of object B, and in B's constructor it has name = n statement. So why b.getName() gets A's name null , not b's name 'Jim' ?'
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mohamed Sanaulla wrote:
Song Guo wrote:
Mohamed Sanaulla wrote:For a) The object in the context is B and not A, hence "this" refers to the instance of B class.
For b) As the name is private in A, its not visible in class B though B extends A. Private members are only visible within the class they are declared. The constructor in B sets the value for B's name and not A's name.
Try declaring the name variable in class A to be protected and remove the name declaration from class B.
In the context above, 'this' is the reference of object B, and in B's constructor it has name = n statement. So why b.getName() gets A's name null , not b's name 'Jim' ?'
Thank you !
As I mentioned in b) that the name variable is private to the class, which means the changes you make to the name variable in class B is limited to that only.
Song Guo wrote:In the context above...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Song Guo wrote:
Mohamed Sanaulla wrote:
Song Guo wrote:
Mohamed Sanaulla wrote:For a) The object in the context is B and not A, hence "this" refers to the instance of B class.
For b) As the name is private in A, its not visible in class B though B extends A. Private members are only visible within the class they are declared. The constructor in B sets the value for B's name and not A's name.
Try declaring the name variable in class A to be protected and remove the name declaration from class B.
In the context above, 'this' is the reference of object B, and in B's constructor it has name = n statement. So why b.getName() gets A's name null , not b's name 'Jim' ?'
Thank you !
As I mentioned in b) that the name variable is private to the class, which means the changes you make to the name variable in class B is limited to that only.
Modify the private in both A and B to public, the output is the same.
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|