aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading" Watch "Overloading" New topic
Author

Overloading

kumaran dhakshna
Greenhorn

Joined: Dec 03, 2004
Posts: 9
hi rancher,

I tested this program i felt the result will be
The value of b is 200
In drived set
The value of b is 200

but the result is
The value of b is 200
In drived set
The value of b is 100


Why this is? Can anybody clear this.


Thank in Advance
[ December 15, 2004: Message edited by: Barry Gaunt ]

SCJP 1.4<br />SCWCD 1.4
Surendra Kumar
Greenhorn

Joined: Apr 20, 2003
Posts: 21
Hello Kumaran,

You can only override methods, not instance variables(t.a).

Cheers
Surendra
Jugal Hans
Greenhorn

Joined: Aug 30, 2004
Posts: 17
You can only override methods, not instance variables(t.a)

If that is the case then when t was created with constructor of the subclass it actually called the base class cosntructor ? is that correct interpretation ?

Thanks
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
In his case, there are two instance variables named "a". The one in Test is initialized to 100 by the Test constructor, which executes first. The one in drived is initialized to 200 by the drived constructor, which executes second.

However, the second "a" is changed to 100 by drived's set() method, which is called from main() in Test with Test's "a" as an argument.

Remember that the hidden first line of the drived constructor is "super();", which calls the Test constructor.


Mike Gershman
SCJP 1.4, SCWCD in process
Kunaal A Trehan
Ranch Hand

Joined: Feb 04, 2004
Posts: 63
Hi

Reason why u get 100 instead of 200 is that in case of variables,dynamic binding is not there.SO when u pass t.a.It sees t is defined as Test t=new Derived();.SO it picks value 100 belonging to Test not 200 belonging to Derived.

I hope it explains.

Rgds
Kunal
 
Don't get me started about those stupid light bulbs.
 
subject: Overloading