File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method invoked" Watch "method invoked" New topic
Author

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
Sheriff

Joined: Aug 26, 2001
Posts: 7610
heavily discussed here
http://www.javaranch.com/ubb/Forum24/HTML/013469.html
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method invoked
 
Similar Threads
Calling a method
Marcus Green Exam 3 Q57
overriding methods, please help
Marcus Green #Exam 3 Q57
Marcus Green#3: Q57 - Underlying Object Type