• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

why a runtime error is thrown if and only if the method someMethod of class One_impl

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface One
{
public void someMethod();
}
public class One_impl implements One
{
public native void someMethod();
}

Assuming that the native method is not provided in any local library, an attempt to compile and run the above lines of code will cause

1. Compilation error - implimentations can never be native.
2. Compilation error - method not found in local libraries.
3. Runtime Exception - method not found in local libraries.
4. Compilation successfull but runtime error is thrown if and only if the method someMethod of class One_impl is called.
given ans:4
I dont know clearly about the given ans 4 enve I got the ans.
why a runtime error is thrown if and only if the method someMethod of class One_impl is called.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because the java compiler does not check or compile native code - these are supposed to be made available to the program whenever the program needs it( thru dlls or something similar). The compiler checks for syntactic correctness, existance and access options for the java code. Since the native code is external to java the program does not check it. So you might well declare a native code, complie your java, build the native application, and then run your code.
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CODE
---------------------------------------------------------------
interface One
{
public void someMethod();
}
public class One_impl implements One
{
public native void someMethod();
}
Assuming that the native method is not provided in any local library, an attempt to compile and run the above lines of code will cause

1. Compilation error - implimentations can never be native.
2. Compilation error - method not found in local libraries.
3. Runtime Exception - method not found in local libraries.
4. Compilation successfull but runtime error is thrown if and only if the method someMethod of class One_impl is called.
---------------------------------------------------------------
Here public class One_impl implements One but it is not providing implementation for method somemethod().
Also the class is not declared abstract.
Should this not throw a compiler error?
Ranchers .. pls explain..
tvs sundaram
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method that is native is never defined as abstract. "native" is supplying the implementation. It is telling you that the implementation is in a native library.
You will actually get a runtime error because no main method is provided...
Ignoring that, you will get:
java.lang.UnsatisfiedLinkError: someMethod
at One_impl.someMethod(Native Method)
at One_impl.main(One_impl.java:10)
Exception in thread "main"
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the correct answer is 4.

------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Just to add my 2 cents.

why a runtime error is thrown if and only if the method someMethod of class One_impl is called.


As Sunetra said, the compiler does not check for native code and the runtime won't bother until the native method is actually invoked. Which is why you won't get an error until someMethod in class One_imp is called.
If the code rarley invokes the native method, it could run for ages without producing any errors (all other things being equal ie that the code is otherwise correct)
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't have to have to implement the abstract method all that you need to do is just define the method. It compiles fine if you just define it if not you get the following error.
One.java:5: class One must be declared abstract. It does not define void someMethod() from interface One.

Originally posted by tvs sundaram:
CODE


 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean native method not abstract.. I am sorry if I confused anybody

Originally posted by Roopa Bagur:
You don't have to have to implement the abstract method all that you need to do is just define the method. It compiles fine if you just define it if not you get the following error.
One.java:5: class One must be declared abstract. It does not define void someMethod() from interface One.


 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic