Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading

 
kumaran dhakshna
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Surendra Kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Kumaran,

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

Cheers
Surendra
 
Jugal Hans
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kunaal A Trehan
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic