Forums Register Login

Overriding,Overloading - Question

+Pie Number of slices to send: Send
[CODE]
class Trebble{
int i = 99;
}
class Base extends Trebble{
int i =100;
}
public class Central extends Base{
public static void main(String argv[]){
Central c = new Central();
c.wynyard();
}
public void wynyard(){
Trebble t = new Central();
Base b = (Base) t;
System.out.println(b.i);
}

}
[/CODE}
1 Compile time error
2 Compilation and output of 99
3 Compilation and output of 100
4 Compilation, but runtime error because an instance of Trebble cannot be cast to type Base

The Correct Answer is 3).This is on what basis?
+Pie Number of slices to send: Send
Variables are referred by type of reference variable and not type of object referred to.

In this case, type of reference variable is b. Value of i in b is 100
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 628 times.
Similar Threads
Thread doubt?
Default constructor !!!!
inheritance question
please help
Reference value doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:37:37.