wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes About Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Polymorphism" Watch "About Polymorphism" New topic
Author

About Polymorphism

Song Guo
Greenhorn

Joined: Apr 24, 2011
Posts: 24




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!



[Thumbnail for output.PNG]

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.


Mohamed Sanaulla | My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Apr 24, 2011
Posts: 24

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

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: Mar 17, 2011
Posts: 7552
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Song Guo
Greenhorn

Joined: Apr 24, 2011
Posts: 24

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

Joined: Mar 17, 2011
Posts: 7552
    
  18

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

Joined: Apr 24, 2011
Posts: 24

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.
 
 
subject: About Polymorphism
 
Similar Threads
Constructor Behaviour
not overrided
Private means
Shadowing in Inheritance
Can abstract class have constructor