Interface with no methods

2019-02-03 12:09发布

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.?

5条回答
Anthone
2楼-- · 2019-02-03 12:50

One of the "clean" features of the Java programming language is that it mandates a separation between interfaces (pure behavior) and classes (state and behavior). Interfaces are used in Java to specify the behavior of derived classes. Often you will come across interfaces in Java that have no behavior. In other words, they are just empty interface definitions. These are known as marker interfaces. Some examples of marker interfaces in the Java API include:

  • java,lang.Cloneable
  • java,io.Serializable
  • java.util.EventListener

Marker interfaces are also called "tag" interfaces since they tag all the derived classes into a category based on their purpose. For example, all classes that implement the Cloneable interface can be cloned (i.e., the clone() method can be called on them). The Java compiler checks to make sure that if the clone() method is called on a class and the class implements the Cloneable interface. For example, consider the following call to the clone() method on an object o:

SomeObject o = new SomeObject();
SomeObject ref = (SomeObject)(o.clone());

If the class SomeObject does not implement the interface Cloneable (and Cloneable is not implemented by any of the superclasses that SomeObject inherits from), the compiler will mark this line as an error. This is because the clone() method may only be called by objects of type "Cloneable." Hence, even though Cloneable is an empty interface, it serves an important purpose.

查看更多
Summer. ? 凉城
3楼-- · 2019-02-03 12:51

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.

查看更多
一纸荒年 Trace。
4楼-- · 2019-02-03 12:54

Marker interface is used as a tag to inform a message to the java compiler so that it can add special behavior to the class implementing it. Java marker interface has no members in it.

The purpose of Marker interfaces is to force some kind of functionality in the classes by providing some functionality to a class if it implements the marker interface.

Read Java Marker Interface also see What is the use of marker interfaces in Java?

查看更多
Lonely孤独者°
5楼-- · 2019-02-03 13:07
registerNatives()

native method are implemented in JVM itself. What does the registerNatives() method do?

Why Java need some method without body to be called from static block.?

This is called from static block because we need to call this method when classes are loaded and not when it's instance is created.

查看更多
何必那么认真
6楼-- · 2019-02-03 13:08

For the first one you are actually asking for a Marker Interface. Marker Interfaces are by design not supposed to add anything to behavior but support only polymorphic transformation of the object. e.g. Serializable makes an object capable of streaming across JVM boundaries. Marker interfaces follow the 'universal type substitution' philosophy.

For second one, you are actually asking for JNI. Java doesnot implement all its code in Java form. I mean in classes and code that follow Java syntax. Some time or the other you need to drill down to the native platform API to implement something for that API. e.g. sockets and TCP communication. It is this feature of Java that actually makes it platform independent. The JVM runtime is platform dependent as it uses platform based native methods and dll or .so libraries to implement and integrate with the platform. We as programmers call the high level Java SDK API calls.

查看更多
登录 后发表回答