0

Consider this example:

public interface TestInterface {
  ParamInterface get();

  void set(ParamInterface param);

  public interface ParamInterface {
  }
}

I want implement both interfaces, like this:

public class Test implements TestInterface {
  private Param param;

  @Override
  public Param get() {
    return param;
  }

  @Override
  public void set(Param param) {
      this.param = param;
  }

  public class Param implements ParamInterface {
      //
  }
}

Why getter is valid, but in setter has error?

Error: The method set(Test.Param) of type Test must override or implement a supertype method

EDIT: I undestand the problem, but I want restrict argument type to Param type. I can solve this example using generics, but if I have more mehtods in same situation, it is not a good solution.

public interface ITest<T extends ITest.IParam> {
    T get();

    void set(T param);

    public interface IParam {

    }
}

public class Test implements ITest<Test.Param> {
  private Param param;

  @Override
  public Param get() {
      return param;
  }

  @Override
  public void set(Param param) {
      this.param = param;
  }

  public class Param implements ITest.IParam {

  }
}
3
  • In setter method: Error: The method set(Test.Param) of type Test must override or implement a supertype method Commented Mar 5, 2018 at 12:54
  • The private field 'param' in your Test class must be type of 'ParamInterface ', because the interface 'TestInterface' methods deal the the type of 'ParamInterface'. It will resolve your exception. Try once. Commented Mar 5, 2018 at 13:05
  • I want restrict argument type to Param. I can solve this with generics, but it I have more methods in the class it will be not a good solution Commented Mar 5, 2018 at 13:09

5 Answers 5

1

Overrided methods must be compatible, see this:

ParamInterface get()

and it's override

Param get()

Both are called get, take no parameters and return a ParamInterface. That's because Param is a ParamIterface

Now take a look at the setter:

void set(ParamIterface param)

and it's override

void set(Param param)

You see? The interface's setter takes a ParamInterface, but it is trying to be overrided by a method that takes a Param. ParamIterface is NOT a Param.

This is analogue to how dogs are mammals but mammals are not (in all cases, at least) dogs.

You could do (on class Test):

@Override
public void set(ParamInterface param) {
    this.param = (Param) param; // Be careful, because this conversion
                                // may now always work (throws an error)
}
Sign up to request clarification or add additional context in comments.

Comments

0

Error: The method set(Test.Param) of type Test must override or implement a supertype method

Your set method from interface void set(ParamInterface param); accepting the types of ParamInterface and your ovverridden method changed that behaviour by accepting set(Param param). You can't do that.

You can pass Param while using the method but not while declaring it. Have the same types in declaration and while implementing and it should work.

Comments

0

void set(ParamInterface param); takes a ParamInterface so the override needs the same signature, not one which takes Param.

The return type is not part of the function signature. See Does a method's signature in Java include its return type? for more details.

Comments

0

There is no co-variance on overridden method arguments, only return types. In other words, you need to parametrize your set method with the ParamInterface type.

The method you are presently trying to override really is an overload.

In other words, your code fails to compile both because:

  1. You have an annotated @Override that does not override the parent's method declaration
  2. You are lacking an implementation of the setter that fits the parent's method declaration

You will ultimately need to decide whether your instance field is of type ParamInterface instead of Param (which would allow your fixed setter code to compile straightforward, but require you either change the return type of your get method to ParamInterface, or use an explicit cast).

Comments

0

The problem is with the type mismatch of the methods of the class Test and the interface TestInterface. See the code below.

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
    }
}

class Test implements TestInterface {
      private ParamInterface param;

      @Override
      public ParamInterface get() {
        return param;
      }

      @Override
      public void set(ParamInterface param) {
          this.param = param;
      }

      public class Param implements ParamInterface {
          //
      }
}

interface TestInterface {
      ParamInterface get();

      void set(ParamInterface param);

      public interface ParamInterface {
      }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.