简体   繁体   中英

Programming by Interfaces

When in interface you declare a method that takes an interface as an argument like

public interface testInterface{
  public void TestMethod(SomeInterface in);

}

When you get to implement this method in a class that implements this interface, does the argument need to be again (SomeInterface in) (most general form) or can it be just an interface that implements SomeInterface ? If the first holds, do I need to cast or what?

Update : I mean if the method signature in the implementing class must have (SomeInterface in) as a parameter or if it can be a specific class implementing (SomeInterface in)

You don't have to cast; you can pass any subclass of your argument as a parameter.

Otherwise, Object's .equals() would never work! It accepts an Object as an argument, but you can do "foo".equals("bar") .

And anyway, you cannot build a new instance of an interface (except if you create an anonymous class). You have to provide an implementation...

在地方的SomeInterface in你可以通过它实现SomeInterface任何类。

As long as the instance you pass into the method TestMethod implements SomeInterface or another interface which extends SomeInterface you are fine. Besides, methods in Java should start lowercase. Instead of casting you may us generics like this:

public interface TestInterface<T extends SomeInterface> {
    public void testMethod(T in);
}

Yes you can pass the references of the class that implements the SomeInterface as the arguments.

like

class test implements SomeInterface{

SomeInterface in=new test();
public void TestMethod(in );
}

I mean if the method signature in the implementing class must have ( SomeInterface in ) as a parameter or if it can be a specific class implementing ( SomeInterface in )

No, the method signature must accept the interface. This fails, for instance:

interface FooInterface {
    public void doSomething();
}

class Foo implements FooInterface {
    public void doSomething() { }
}

interface BarInterface {
    public void myMethod(FooInterface f);
}

class Bar implements BarInterface {
    public void myMethod(Foo f) { }
    //                   ^----------------- Note
}

If you try to compile that, you get:

Bar is not abstract and does not override abstract method myMethod(FooInterface) in BarInterface
    class Bar implements BarInterface {

But if you define Bar.myMethod as accepting FooInterface , you can (of course) call it with Foo instances, as that's the point of interfaces.

You can precisate 3 parts of a interface-method-declaration

  • Return type (impl's type must inherit interface's type)
  • Exception type (interface's type must inherit impl's type or impl's type must inherit interface's type)
  • Parameter types (impl's type must inherit interface's type)

Example:

interface Test {
    Number test(CharSequence v) throws IllegalArgumentException;
}

Impl:

public class TestImpl implements Test {
    public Integer test(final String v) throws NumberFormatException {
        return null;
    }
}

Note:

Parameters using ... -arrays can inherit [] -arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM