Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding,Overloading - Question

 
Vidyavathi saravanan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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?
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic