• 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

Inheritance Doubt

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When I execute the foll code, the output is 10 & sub.Can anyone please explain?
Thanks in Advance,
Usha.
class A
{
int i = 10;
void method (){ System.out.println ("Super"); }
}
class B extends A
{
int i = 12;
void method () { System.out.println("Sub"); }
}
public class test
{
public static void main (String args[ ])
{
A obj = new B();
System.out.println(obj.i);
obj.method();
}
}
 
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because, variables are bound at compile-time and methods are bound at run-time .
When the compiler sees the statement
A Obj = new B();
It binds its variable i at compile-time and will not resolve the method call until execution time
so, you get this ouput!
Hope it is clear!
Sudharsan
[ January 31, 2003: Message edited by: Sudharsan G'rajan ]
 
usha raju
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops...This is getting even more confusing !! You are contradicting your own stmt...In the first line you say variables are bound at run-time and methods are bound at compile-time whereas in the next line you say just the reverse thing.
Please make it clear.
Thanks so Much !!
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables: Compile time
Methods: Run time.
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh! I'm sorry. I typed it wrong.
 
reply
    Bookmark Topic Watch Topic
  • New Topic