Forums Register Login

Inheritance Doubt

+Pie Number of slices to send: Send
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();
}
}
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 !!
+Pie Number of slices to send: Send
Variables: Compile time
Methods: Run time.
+Pie Number of slices to send: Send
oh! I'm sorry. I typed it wrong.
I promise I will be the best, most loyal friend ever! All for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 621 times.
Similar Threads
Inheritance
Does static methods and variables get inherited ?
Help plz..
var args method
Inheritance
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:52:19.