English 中文(简体)
Java : How to create a function that can only accept object which is an Interface?
原标题:

I need to write a function that accepts an object , but I want to enforce in the function call (not after the function is called) that the object is an interface.

Meaning , I want to make sure this is an Interface at compile time , not on run time.

What do I mean?

interface ISomething {...} 
class A implements ISomething { ... }
ISomething something = new A(); 
MyClass.register(something);

In this example , the passed object is an interface , and I want that the MyClass.register function to enforce the this requirment in it s declaration.

I don t know which interface I m going to get , and there is no use defining another interface to be implemented by all other implementation , because I need the real interface.

最佳回答

You can ask the class of the object you get interfaces it implements during runtime. If you can t already give the compiler the types of the interfaces you expect it has no way to predict what is going to be passed into your method so you will have to use runtime reflection.

问题回答

To accept only objects that implement an interface as argument, use the interface as type, i.e.:

void acceptMaps(Map argument) {
}

can be called with objects implementing the Map interface, like HashMap but not with Strings for instance as they do not implement Map.

Is this what you meant with your question?

Edit in this example, objects implementing SortedMap which extends Map are accepted too, so in your case you could create a BaseInterface and extend that in the interfaces (like AcceptedInterface extends BaseInterface) you want to be accepted by your .register(BaseInterface arg) method.

You can t instantiate an interface so you would never be able to create one to send in. You can specify an interface in the parameters for the function and only objects which implement that interface can be passed in. But there is no way to require an interface be what is passed in because you can t create them.

I think you need to rethink what you re trying to accomplish.

You re making a distinction between the type of the object (in this case A) and the type of the reference to the object (in this case ISomething).

Sounds like you want to permit this code:

ISomething something = new A(); 
MyClass.register(something);

but forbid this code:

A something = new A(); 
MyClass.register(something);

I don t think you can achieve this with Java.

Let me see if I understand.

Do you want to check at compile time that the argument passed to a function is some interface? Any interface?

If that s the question, the answer is you can t.

I don t know which interface I m going to get [...] I need the real interface.

You can t actually validate if you don t know which type to expect.

In Java you need to know the type to validate the parameter, the argument must be of the same type or a descendant, Java doesn t make distinctions on this regard at compile time, you can make it at runtime as Daff aswered.

There s no way to check at runtime, if the object is an interface because an object can never ever be an interface , it only be an instance of a class that implements an interface.

And it s not possible to restrict a method signature to interface usage, say you ll allow type Animal but not type Dog which implements animal behavior. (I guess that s what you were looking for)

Taking your example - you want a compiler error for this implementation:

interface ISomething {...} 
class A implements ISomething { ... }
ISomething something = new A(); 
MyClass.register(something);

A unwanted = (A) something;
MyClass.register(unwanted); // <- compilation error here

But practically spoken - I see no immediate reason. If you want to enforce programmers to use interfaces - user code inspection or quality check tools. If you want to restrict instantiation of an implementation, protect the constructor and use a factory to produce instances.

"The object is an interface" doesn t make sense. It seems like you want to enforce that the pointer passed into the function was declared with an interface type like

Interface_t x = new Class_which_implements_interface_t();

as opposed to

Class_which_implements_interface_t y = new Class_which_imlements_interface_t();

The only problem is that if you make a function like this:

void some_func(Interface_t z) {...}

And you call it with some_func(x); or some_func(y); the function is passing the reference by value, which means that inside of some_func, z is a copy of x or y which has been casted to an Interface_t pointer. There is no way to get information about what type the original pointer had. As long as it is able to be casted to an Interface_t it will compile and run.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签