Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard class as variable in constructor

I have class A

class A{}

class A1 extends class A

class A1 extends A{}

now in class B, I want to pass any class that extends class A as a variable.

class B{
    Class<? extends A> variable;

    public B(Class<? extends A> variable) {
        this.variable = variable;
    }
}

to run above, the driver class is as below

 class C {
    public static void main(String args[]) {
        B b=new B(new A());// A cannot be converted to Class<? extends A>.
        B b=new B(new A1());// A1 cannot be converted to Class<? extends A>.
            }
}

because Class<? extends A> variable it means, any class extending class A can be passed in the constructor and A1 is extending A, then why its giving error?

what am I missing?

like image 872
shrikant.sharma Avatar asked Jun 14 '26 03:06

shrikant.sharma


2 Answers

I suspect that what you're actually trying to do is to set up B so it can work with instances of A or any subtype of A. To do that, you'll need to make the class B itself generic:

class B<T extends A> {
    T variable;

    public B(T variable) {
        this.variable = variable;
    }
}

class C {
    public static void main(String args[]) {
        B<A> b = new B<>(new A());
        B<A1> b1 = new B<>(new A1());
    }
}
like image 171
Daniel Pryden Avatar answered Jun 15 '26 16:06

Daniel Pryden


Class<? extends A> variable means that variable can hold a reference to an object of type Class, not to something that extends A.

Class is a "meta-class", its purpose is to describe another class. From a Class instance you can get all the fields and all the methods that this class provides. You can take a look to the Java reflection tutorial for more informations.

To make your code work you can write something like this:

class B<T extends A> {
    private T variable;
    public B(T variable) {
        this.variable = variable;
    }
}

class C {
    public static void main(String args[]) {
        B<A> b = new B<>(new A());
        B<A1> b1 = new B<>(new A1());
    }
}

In this way you're creating a generic class B which accepts instances of A or something that extends A

like image 30
MscG Avatar answered Jun 15 '26 16:06

MscG