• 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

Reference and object

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i have one problem..
first see this code
class A
{
int i=10;
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
class B extends A
{
int i=20;
public static void main(String[] args)
{
A o1=new B();
System.out.println("Hello World!"+o1.i);
}

}
when i am pritning value of i it is giving me 10
can any one help me
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amod gole:
hi all,
i have one problem..
first see this code
class A
{
int i=10;
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
class B extends A
{
int i=20;
public static void main(String[] args)
{
A o1=new B();
System.out.println("Hello World!"+o1.i);
}

}
when i am pritning value of i it is giving me 10
can any one help me



Instance variables in a subclass do not override instance variables in the superclass with the same name, they hide them.

If you have an object of type B that is referred to with a reference of type A, then if you access the instance variable with that reference, you will get the value of the variable in A.
 
amod gole
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi and thanks for reply,

and yes i know that it is hiding instance variable of super class
but why,as i assinging object of derived class to refernce of base class
that means at run time that reference points to or contains object of dervied so first it should check in derived if that variablenot exists in dervied then it should move to base.

i am not getting clear.
it is confusing me bet_n datamember and memberfunction
why different behavior
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
8.3.3.2 Example: Hiding of Instance Variables
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic