This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Legal Method Declarations

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Mortimer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Dan Mortimer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim!
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No worries, Cheng...thanks for your help!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic