• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

About Polymorphism

 
Greenhorn
Posts: 24
Redhat Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



a) When a B object is created, first the default constructor of A will be called, but why the output is "In A's default constructor: B@...", not 'A@...' ?
b) Though A's default constructor is called and its name field is null, in B's constructor it has 'name = n'. Why b.getName() is null ?
Thank you!
output.PNG
[Thumbnail for output.PNG]
output
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 80061
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know you will call me pedantic, but you do not have a default constructor. You only get default constructors when you don’t write a constructor yourself.
 
Song Guo
Greenhorn
Posts: 24
Redhat Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Actually, you're wrong on two counts:

1. The only this that has any bearing on events is the this defined in A.getName(), and in that context it is a reference to an object of type A (a B object IS an A, remember).

2. When you call b.getName(), you are actually calling A.getName(), since that's the only method you've defined; therefore polymorphism doesn't even come into the picture.

Winston
 
Song Guo
Greenhorn
Posts: 24
Redhat Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Song Guo wrote:In the context above...


Song,

Please DontWriteLongLines (←click) inside code blocks. I've broken up the one of yours that was causing problems this time.

Thanks.

Winston

 
Song Guo
Greenhorn
Posts: 24
Redhat Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Problem solved. Thanks.
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic