• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

help

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Plz explain the following concept.

The type of object reference determines the variable, the type of underlying object determines the method.

thanks
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !
If i have understood correctly this ought to be with reference to a subclass - superclass situation. If that is so then the concept is that : it is the type of reference that will determine "WHAT" var/members can be accessed. But it is the underlying object "WHOSE" variables and methods will be accessed. Ok did i confuse u even more... here's an eg
class A
{
int x;
A()
{
x=10;
}
public void meth1()
{
// some code
}
}
class B extends A
{
int y;
B()
{
x=20;
y=30;
}
public void meth1()
{
//
}
public void meth2()
{
//
}
}
class C
{
public static void main(String args[])
{
A a1=new B();
System.out.println(a1.x);
/*above will print 20 i.e the value defined in underlying object B()*/
System.out.println(a1.y);
/* above will give compiler error - saying undefined var - as y has not been declared in the superclass, and it is the reference (i.e. a1 here) which decides which members can be accessed. In short a1 will only be able to access all the var/ methods defined in A but the actual values of these var/ methods will be taken from B. Similarly u could go ahead with the methods (In case of overiding - the same principle applies)*/

}
}

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic