• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Dan's Exam

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic