• 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

interface question

 
Ranch Hand
Posts: 417
  • 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.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't really an interface question, it's an implementation question.
The interface simply defines a particular method signature.
In the class, you are implementing that method. You are using a native method for the actual implementation. When you compile, the native keyword tells the compiler "Hey, don't worry about compiling this method, it's not written in java. I promise you'll find it *someplace* when I run it. Honest."
But since you haven't actually written an implementation for it, or loaded a library, if you try to call it, the VM won't be able to find the implementation, so it won't be able to run that method, so you'll get a runtime error.

If you never try calling the method, then you'll never have a problem, because the VM won't have to look for the method implementation.

Rob
[ January 30, 2002: Message edited by: Rob Ross ]
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so the answer is 3 or 4 ?
because now we know that there cannot be any compilation problem as the compiler has not specifically told about it.

Originally posted by Rob Ross:
This isn't really an interface question, it's an implementation question.
The interface simply defines a particular method signature.
In the class, you are implementing that method. You are using a native method for the actual implementation. When you compile, the native keyword tells the compiler "Hey, don't worry about compiling this method, it's not written in java. I promise you'll find it *someplace* when I run it. Honest."
But since you haven't actually written an implementaion for it, and loaded a library, if you try to run it, the VM won't be able to find the implementation, so it won't be able to run that method, so you'll get a runtime error.

If you never try calling the method, then you'll never have a problem, because the VM won't have to look for the method implementation.

Rob

 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is 4
Rob.
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
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