Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method invoked

 
fengqiao cao
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, there
The following compile as it is(output: Agg), but when i comment lin1 to line 4. it won't compile. could anyone explain ?
as far as i know, "the method invoked depends on the object itself, not on the declared type".
class Base {
public String getFields(){//line 1
String name="base";//line 2
return name; //line 3
}//line 4
}
class Agg extends Base{
public String getFields(){
String name = "Agg";
return name;
}
}

public class Avf{
public static void main(String argv[]){
Base a = new Agg();
System.out.println( a.getFields());//carefull
}
}
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"the method invoked depends on the object itself, not on the declared type".
This is true for non static, non private, non constructors non super.method() invocations at runtime.
At compile time the compiler must make certain the existance of a declaration in the declared type that matches the invocation made.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
heavily discussed here
http://www.javaranch.com/ubb/Forum24/HTML/013469.html
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic