aspose file tools*
The moose likes Beginning Java and the fly likes Legal Method Declarations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Legal Method Declarations" Watch "Legal Method Declarations" New topic
Author

Legal Method Declarations

Dan Mortimer
Greenhorn

Joined: Jun 01, 2004
Posts: 18
I am working towards my Sun Java certification by working through the Java 2 study guide, written by Kathy Sierra and Bert Bates. So far, I have found the book to be an excellent study tool - particularly because of the exam questions it contains. I am having trouble with one of the exam questions though, so I am wondering if anyone can provide assistance. The question I am having difficulty with is in the self-test section at the end of Chapter 2. Please see below:

Qustion 5
How many of the following are legal method declarations?
i) protected abstract void m1();
ii) static final void m1() {}
iii) transient private native void m1() {}
iv) synchronized public final void m1() {}
v) private native void m1();
vi) static final synchronized protected void m1() {}

The answer given in the book is that five of the above method declarations are correct and that only the third is incorrect (because the keyword transient can only be applied to variable declarations).

I think that the fifth method declaration is also incorrect because it is not marked as abstract and yet it ends with a semicolon and not a pair of curly braces. Please could somebody confirm whether my thoughts are correct and offer any supporting explanation that will help me to understand this question better.

Any help much appreciated!
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
There's nothing wrong with the 5th option. All methods in an abstract class are abstract. So it works even if you didn't put in the abstract modifier.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Dan Mortimer
Greenhorn

Joined: Jun 01, 2004
Posts: 18
Hi Cheng,

Thanks for your help!

Am I right in saying that you can have non-abstract methods in an abstract class...?

Based on your answer, is it true to say that abstact methods don't have to have the abstract modifier and therefore, the only sure way to recognize a non-abstract method in an abstract class is that it must provide a method body within curly braces?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[CWL]: There's nothing wrong with the 5th option. All methods in an abstract class are abstract. So it works even if you didn't put in the abstract modifier.

Ah, no, this is not correct. An abstract class can contain both abstract and non-abstract methods. Perhaps you're thinking of an interface? All method in an interface are abstract, whether declared as such or not.

Regardless though, this has nothing to do with the answer to D's question. The key is that option v is a native method. This means that the code will be defined elsewhere on the system, perhaps using C or some other language. The Java code for a native method always omits the body. If the method had a body, it would be completely defined in that Java class, and there'd be no reason to declare the method as native. Although the missing method body looks similar to an abstract method, it is not the same thing. An abstract method will be defined in a concreate subclass, in Java. A native method will be defined by some other programmign language, not Java.


"I'm not back." - Bill Harding, Twister
Dan Mortimer
Greenhorn

Joined: Jun 01, 2004
Posts: 18
Thanks Jim!
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
I'm so sorry. I wasn't thinking

Option 5 is not an abstract method at. You can have a simple "Hello World" Java class with a private native void method as in option 5. I tried it & it is able to compile & run successfully.
Dan Mortimer
Greenhorn

Joined: Jun 01, 2004
Posts: 18
No worries, Cheng...thanks for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Legal Method Declarations