• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Overriding

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Foo
{
int a=3;
void Add()
{
a+=5;
System.out.print("f");
}

}

public class Bar extends Foo
{
int a=8;
void Add()
{
this.a+=5;

System.out.print("b ");
}

public static void main(String[] ar)
{
Foo f=new Bar();
f.Add();
System.out.print(f.a);
}

}

in above piece of code if i invoke Add() with the instance "f"

it calls Add() of "Bar"

but if i call "f.a" it uses the instance variable of Foo

as per me it should print b13 isatead of b3.

Thanks
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When you have a declaration for an instance method in a subclass that has the same name and argument list as an instance method in a superclass, you are attempting (you also must abide by the rules for proper overriding in order for this to be a successful override) to override the method.

However, when you have a declaration for a variable in a subclass that has the same name as a variable in the superclass, you are not overriding it (same goes for static methods.) In that case, you just define an additional variable (or static method) that will shadow the superclass' namesake.

The only factor that comes into play when you want to figure out what variable you are retrieving is the type of the reference you are retrieving it through (because the overriding mechanism is not active for variables or static methods.) So, above, f is of type Foo, and therefore f.a will retrieve the variable a defined in Foo. If you do ((Bar)f).a, then you will pick up the variable a defined in Bar.

(To clarify: If a weren't declared in Bar, ((Bar)f).a would still retrieve the a defined (defined as in visible) in Bar. It's just that, in that case, Bar would inherit the a from Foo, without declaring a new variable a, and shadowing the one declared in Foo.)

The main thing to remember is that for variable and static method references you are just retrieving them from a declaration scope (the type of the reference) at compile time, whereas for instance methods, you use the overriding mechanism to decide at run time, based on the type of the actual object the reference variable is bound to, which version of the overriding method set to invoke.

I hope that clarifies things a little.
[ January 02, 2009: Message edited by: Ruben Soto ]
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak can you please QuoteYourSources...
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ruben

it's helps a lot..

and Ankit i cann't tell you the exact source where this Question fetched from

as i have a pile of Question sets given by my institute.

it's one among them
 
He's giving us the slip! Quick! Grab this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic