• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Overloading

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Kumaran,

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

Cheers
Surendra
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic