wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance Doubt" Watch "Inheritance Doubt" New topic
Author

Inheritance Doubt

usha raju
Greenhorn

Joined: Jan 30, 2003
Posts: 3
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

Joined: Jul 03, 2002
Posts: 319
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 ]

Joy is a radiation
usha raju
Greenhorn

Joined: Jan 30, 2003
Posts: 3
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

Joined: Mar 11, 2002
Posts: 1140
Variables: Compile time
Methods: Run time.
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
oh! I'm sorry. I typed it wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance Doubt
 
Similar Threads
Does static methods and variables get inherited ?
var args method
Help plz..
Inheritance
Inheritance