• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract native

 
Ram Reddy
Ranch Hand
Posts: 88
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i declared a method as abstract and native.
But in eclipse i am getting compile time error, native method should be public or protected.

code as follows

package Test1;

public abstract class Parent
{
protected int i = 10;
public void goUpHill(){};

protected abstract native void cImplementation1();

public abstract native void cImplementation3();
}


1) we cannot declare a native method as abstract?

2) but it is allowed me to declare as private method without implementatioin.

private native void cImplementation();

when the implemetation for this provided?
 
Chandu Sree
Greenhorn
Posts: 22
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

1) Abstract and native cannot be applied together as abstract means its implementations is provided in the subclass(that is no implementaion is provided and next implementing class has to provide) but in case of native ,we mean that implementation is already provided just use it.So there is no meaning in specifying both together.

2)As I said native implementation is already provided ,you can use private modifier to it.

Hope this would help you


Best Regards
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks chandu.

But can any one tell me how the JAVA API calls the native methods and will be implemented them.?

i am some what confusing about native methods , how java can implement these methods internally?

thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rami marri:
thanks chandu.

But can any one tell me how the JAVA API calls the native methods and will be implemented them.?

i am some what confusing about native methods , how java can implement these methods internally?

thanks


It's just a different call stack. Normally, methods are compiled to byte codes and it is part of a class. Native methods are different in that they are not directly part of the class -- instead the JVM looks for them, in libraries, which has been loaded eariler.

To write your own native methods, you simply run your java class file (with the native methods specified) through the javah program. This will generate you a C/C++ include file -- which declares the interface for you to code the method in C/C++.

You then compile your function into a share library -- and likely go back to the original class to add a static initializer to load that library.

Henry
[ November 27, 2007: Message edited by: Henry Wong ]
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rami marri:
But can any one tell me how the JAVA API calls the native methods and will be implemented them.?

i am some what confusing about native methods , how java can implement these methods internally?


Hi Rami,

You'll have to read up on the Java Native Interface (JNI) to answer your question. This isn't a simple topic, so here's a link to an old but still useful page describing the basic mechanisms involved:

http://www.acm.org/crossroads/xrds4-2/jni.html

Note that JNI is way beyond the scope of the SCJP, so you definitely don't have to know any of this for the exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic