Search code examples
javaclassinterfacejava-native-interface

Interface with no methods


Why do Java introduces some interface which has no methods defined in it? For example Cloneable, Serializable, Type and many more.

Second thing : In Class.class package there is one method defined registerNatives() without body and is called from static block but Class.class is not abstract but is final. Why so? and Why Java need some method without body to be called from static block.?


Solution

  • Why do Java introduces some interface which has no methods defined in it?

    This are called Tagged or Marker interface. These are not used for any use or operation. These methods are used to tag or marking a class. So that you can determine whether someclass is a child of those classes.

    about the second question

    If you look closely you can see the declaration is

     private static native void registerNatives();
    

    So registerNatives is a native methods.

    So what is native methods. If you see this so question

    The method is implemented in "native" code. That is, code that does not run in the JVM. It's typically written in C or C++.

    Native methods are usually used to interface with system calls or libraries written in other programming languages.

    So these methods are loaded from native codes. So you don't need to declare the body of the methods but still they are not abstract as they have their implementation from native codes.