• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance Doubt

 
usha raju
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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();
}
}
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 !!
 
Mani Ram
Ranch Hand
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables: Compile time
Methods: Run time.
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh! I'm sorry. I typed it wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic