Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interesting behavior Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interesting behavior" Watch "interesting behavior" New topic

interesting behavior

Kirti Dhingra
Ranch Hand

Joined: Dec 13, 2000
Posts: 32
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();
output is is
lower ft
upper gt
rajani peddi
Ranch Hand

Joined: Nov 27, 2000
Posts: 73
In the first case the method is being ovverriden and in the second case its not. Hence the behaviour.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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().
I agree. Here's the link:
subject: interesting behavior