This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 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().
wood burning stoves
subject: interesting behavior
jQuery in Action, 3rd edition