Can I rename implemented methods in Java?

I have a class that implements the interface, and one of its methods is called onclick Is there a way to implement onclick that the interface wants, but name it something else? Like (what I'm doing):

public void AnyMethodNameIWant() implements Interface1.onClick

The three reasons I asked are:

>Take a look at the method signature and know that it is good from the interface > to avoid the "common" name that the interface may require me to have like onclick > distinguish the same method name in many interfaces

If this is a basically "bad" problem, I'm sorry because I'm not familiar with Java

Solution

No, you can't The interface must be implemented through a method with the same name in Java

You can use the @ override annotation with the interface implementation (starting with Java 6), which helps clarify that this is a method that cannot be renamed at will

One option for the second problem might be to create an implementation class just to forward calls to more specific methods You may want to treat this as a nested or even anonymous class I'm not sure I usually do that

Edit: I've seen the third question - no, if you have two interfaces with the same method signature in Java, you can only provide one implementation: (oh, if you have two interfaces with the same signature but different return types, what's worse, you can always write an interface1 getinterface1() method, which returns an instance of an anonymous inner class to the "main" class, Proxy interface1 method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>