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

interesting behavior

 
Kirti Dhingra
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class upper{
void ft(){
System.out.println("upper ft");
}
void gt(){
System.out.println("upper gt");
}
}
class lower extends upper{
void ft(){
System.out.println("lower ft");
}
public static void main(String args[]){
upper c=new lower();
((upper)c).ft();
((upper)c).gt();
}
}
output is is
lower ft
upper gt
 
rajani peddi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first case the method is being ovverriden and in the second case its not. Hence the behaviour.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your cast (upper)c is redundant since you already upcasted this to upper when you said:
upper c = new lower();
You are going to get the overridden methods since c is really a lower().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic