0

I have a parent class, Parent, with two child classes, A and B. A have a class, Wrapper, that contains an interface, Function, which allows the programmer to specify a particular method for each wrapper.

Wrapper contains the variables, Class<Parent> inputClass and Class<Parent> outputClass, which specify the input and output type. Wrapper is supposed to map an A to an A or B, or a B to an A or B.

My problem arises when I try to call the wrapper constructor, Wrapper(Class<Parent> input, Class<Parent> output, Function func). Eclipse gives me an error for trying to put an A.class or B.class in for either input or output. Given that A and B are child classes of Parent, I thought they would be able to pass into the constructor. How can I implement this?

public class Parent {
    protected int value;
    public void setValue(int x){ value = x; }
    public int getValue(){ return value; }
}


public class A extends Parent{
    public A(){ setValue(1); }
    public A(B b){ setValue( b.getValue()); }
}

public class B extends Parent{
    public B(){ setValue(2); }
    public B(A a){ setValue(a.getValue()); }
}


public class Wrapper{
    protected Class<? extends Parent> inputClass;
    protected Class<? extends Parent> outputClass;
    protected interface Function{
        public Parent of(Parent input);
    }
    protected Function function;
    public Wrapper(Class<? extends Parent> in, Class<? extends Parent> out, Function func) {
        inputClass = in;
        outputClass = out;
        function = func;
    }

    public Parent of(Parent input){
        try{
            Parent output = function.of( inputClass.getConstructor( input.getClass() ).newInstance(input) );
            return outputClass.getConstructor( output.getClass() ).newInstance( output );
        } catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
}

public class Main {
    public static void main(String[] args){
        ///Error occurs here. Can't take A.class in for Parent.class
        Wrapper wrapper = new Wrapper(A.class,B.class, new Function(){
            @Override
            public Parent of(Parent p) {
                return null;
            }
        });     
    }
}
9
  • I think Class<Parent> in should be more like Class<? super Parent> in, but I've not tried, so you might need to fudge it a bit more (you'd have to apply it to both parameters) Commented Oct 8, 2018 at 3:51
  • 1
    I think you need <? extends Parent> in your Wrapper constructor so that sub-classes are permitted. Otherwise you are restricting in and out to Parent only. Commented Oct 8, 2018 at 3:51
  • Possible duplicate Java generics How to accept any derived type in generic parameter Commented Oct 8, 2018 at 3:52
  • @dave I made those changes but it's still giving me the same error. I had actually tried that before posting. Commented Oct 8, 2018 at 3:59
  • @user2303321 Can you double-check? I copied your latest code into IntelliJ and it compiled without error (with Java 8) for me. Commented Oct 8, 2018 at 4:06

1 Answer 1

1

The problem wasn't from A.class or B.class, but from the interface Function being nested within the Wrapper class. I needed to write new Wrapper.Functionin the constructor in main.

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

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.