Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Class<? extends Something>?

Tags:

java

SolrServer.java have "add()" methods. But, "server.add()" is failed. is If I use as follows:

in my abstract class:

public abstract void addDocs( Class<?  extends SolrServer> server ) throws IOException, SolrServerException;

in my Implementataon class:

@Override
    public void addDocs(Class<? extends SolrServer> server) throws IOException, SolrServerException {
        ...
        server.add(doc);        
    }

enter image description here

No problem, If I use as follows:

@Override
    public void addDocs(SolrServer server) throws IOException, SolrServerException {
       ...    
       server.add(doc);        
    }

enter image description here

What is wrong?

UPDATE:

SolrServer.java is an abstact class. For this reason, the problem could be?

like image 331
cguzel Avatar asked Aug 15 '26 11:08

cguzel


1 Answers

a) if you just use this:

public void addDocs(SolrServer server) throws IOException, SolrServerException {
    ...
    server.add(doc);        
}

you should be able to call this method with any Sub-class of SolrServer as well

b) you could also define a Type Variable:

public <S extends SolrServer> void addDocs(S server) throws AllKindsOfStuff{
    ...
    server.add(doc);        
}

However, you are trying to operate on classes, not on Objects. Change the parameter type from Class<? extends SolrServer> to SolrServer and you should be fine

Ok, since you have a base class, declare the Type Variable in that base class:

public abstract class BaseClass<S extends SolrServer>{
    public abstract void addDocs(S server);
}

public class ImplementingClass extends BaseClass<SomeSolrServer>{
    public void addDocs(SomeSolrServer server){
        // do stuff here
    }
}
like image 80
Sean Patrick Floyd Avatar answered Aug 18 '26 01:08

Sean Patrick Floyd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!