0

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)

0

6 Answers 6

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

To whoever it was who kept trying to fix this for me but I kept overlapping with: Thank you. Such a silly, crucial, typo.
1

In place of SomeInterface in you can pass any Class that implements SomeInterface.

Comments

1

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);
}

Comments

1

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 );
}

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.