GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Exam 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 "Dan Watch "Dan New topic
Author

Dan's Exam

Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
Question 24



A Compile-time error is generated at which line?
a. 1
b. 2
c. 3
d. 4
e. None of the above

Hi!
I am confused about the answer to the above question.The correct answer stated by Dan is (e ).
But I think the answer is d.According to what I interpret the method m1 declared in line 4 is abstract since it does not have a body.
Also the modifier native cannot be combined with the modifier abstract. I am not sure if my assumption that the method in line 4 is abstract is correct or wrong.
Please correct my assumption if I am wrong.
So my question is if a method does not have a body,is it not implicitly abstract?
Please help.
Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
Hi sagarika,

The Class G is expected to define the method m1 . But I guess the 'native' word indicates that the method is implemented in another language. So this should not give a compiler error.
Please corerct me if I am wrong.


Thanks,<br />Lakshmi.
Derek Baker
Ranch Hand

Joined: May 23, 2003
Posts: 46
The method in G is not abstract (it doesn't use the abstract keyword), it just has an empty, do-nothing implementation. So native is an acceptable modifier.
If a method in a class does not use the keyword abstract, it is not abstract. It can have an empty implementation but that doesn't make it abstract.
[ December 12, 2003: Message edited by: Derek Baker ]
Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
Hi Derek
I think the do nothing implementation or empty implementation should be having an empty body as follows:
public native void m1(){}
But in the problem above it is not an empty implementation but instead it is not implemented at all i.e the method ends with a semicolon
as follows:
public native void m1();
That is why I am confused.
And also Dan states in another explanation that native and abstract should not be used together.
So when the method here is not given an implementation body at all how can we say that it is implemented by the class.And we know that interface methods are implicitly abstract and unless the class which imlements the interface provides an implemantation body how can the abstract keyword be removed?I am missing some point here I guess.
I am still not so clear about the explanation.If somebody could give a better explanation it would be great.
Thanks anyways Derek and Lakshmi.
Derek Baker
Ranch Hand

Joined: May 23, 2003
Posts: 46
You're right. I misread your original post. I still, though, don't think that a method in a class (not an interface) is abstract if you don't explicitly declare it that.
A method declaration that doesn't specify native won't compile if it is only a semicolon. But when it does specify native, it compiles fine. I think there are implications to declaring a method native that I don't understand.
Kashif Memon
Ranch Hand

Joined: Jul 12, 2003
Posts: 35
Originally posted by Sagarika nair:
So when the method here is not given an implementation body at all how can we say that it is implemented by the class.And we know that interface methods are implicitly abstract and unless the class which imlements the interface provides an implemantation body how can the abstract keyword be removed?I am missing some point here I guess.
I am still not so clear about the explanation.If somebody could give a better explanation it would be great.
Thanks anyways Derek and Lakshmi.

public native void m1(); this shows that the method has been implemented but its implementation is not in Java language.
native keyword shows that the body of the method is in some other language such as C or C++, external to JVM. The standard API (JNI) is used to implement native methods in C.
do correct me if I m wrong.
Regards,
Kashif Memon
Sumitro Palit
Ranch Hand

Joined: Dec 13, 2003
Posts: 37
Guys,
Remember that if you declare a method as native, then you cannot provide an implementation.
So it has to be like :
native void foo();
Trying:
native void foo(){}
would give a compilation error, because the implementation is supposed to be in a "native" code like C/C++.
Mind you, native void foo(); will compile, but at run time, the native implementation must be available or else there will be a runtime error.
Readup on JNI to see what it takes to implement java methods in native c/c++ code. The details are not necessary for the SCJP exam, but its worth knowing the basics.
Check this link for a very brief introduction to JNI.
http://www.geocities.com/kongwenyu/jni.html
- Ortimus
Seshachala Dharmaveer Kalluri
Greenhorn

Joined: Dec 16, 2003
Posts: 2
Hi Sagarika,
The main point to be noted is that, abstract and native methods should not have a body for them. They are supposed to be defined elsewhere.
Abstract methods will be defined in subclasses, where as native methods will be defined entirely outside the JVM. That is why a method cannot be both native and abstract.
so, the class implementation in the given example,
class G implements Z {public native void m1();} is perfectly correct. If u remove that ; and replece it with {}, it will give a compiler error saying Native methods can't have a body. Hope this removes ur confusion.
Dharmaveer
 
GeeCON Prague 2014
 
subject: Dan's Exam