• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Polymorphism

 
Song Guo
Greenhorn
Posts: 24
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • 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
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • 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
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic