0

I have an abstract base class that has the following private variable

private Map<String, ?> options;

I want every other class that will extend my base class to implement the following method

protected abstract void initOptions(Map<String,?> options)

my problem is that I can't choose any other type than Map for the implementation

@Override
protected void initOptions(Map options) throws InternalLoginException {
    ...
}

What is the proper way to handle such a situation where I do not have control over the type of the options Map but I want to let the implementing class of the initOptions method know that the key in the Map is of type String and the value could be any Object.

dasd-431
  • 47
  • 1
  • 6
  • I'm not sure what you're trying to do by creating a private variable in the abstract class and not using it anywhere in abstract class + not exposing it via public method + no way to set this parameter. I was able to achieve whatever you've written here in my local IntelliJ IDE. You must be doing something wrong when writing the code. What is the error you're getting ? – Aman May 03 '22 at 14:42
  • 1
    For me, it is unclear what you mean by "*my problem is that I can't choose any other type than Map for the implementation*". We defined the abstract method to take a `Map<...>`, hence we must implement a method with that exact signature in all children. Please [edit] the post and clarify the question. --- Please read: [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Turing85 May 03 '22 at 14:42

1 Answers1

2
public abstract class BaseClass {

    abstract void methodToOverRide(Map<String, ?> parameter);


    static class SubClass extends BaseClass{

        @Override void methodToOverRide(Map<String, ?> parameter) {

            //do something?
        }
    }
}

What you have should work as shown above.

JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • thank you. I just found out that the problem was something else. BaseClass had a generic parameter T. That parameter is not used in the map, yet it gave me this error message: "'methodToOverRide(Map)' in 'BaseClass.SubClass' clashes with 'methodToOverRide(Map)' in 'BaseClass'; both methods have same erasure, yet neither overrides the other" defining Subclasses type solved the issue. – dasd-431 May 03 '22 at 15:33