Two Laptop Bag
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method invoked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method invoked" Watch "method invoked" New topic

method invoked

fengqiao cao
Ranch Hand

Joined: Oct 26, 2001
Posts: 71
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

Joined: Jul 03, 2001
Posts: 2120
"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.

SCJP2. Please Indent your code using UBB Code
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
heavily discussed here
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: method invoked
It's not a secret anymore!