Can I declare method to return Class object with restriction to subclasses of given type in Java? -


suppose declare abstract method, return class object (like dog.class, cat.class), this:

abstract public class getproducedanimaltype(); 

can force clients extending api return class objects represents classes, wchich subclassess of animal.class ? like:

abstract public class<representing subclass of animal> getproducedanimaltype(); 

so client extending class cannot write:

public class getproducedanimaltype() {    // integer not extends animal!, shoud not compile!    return integer.class;  } 

but can:

public class getproducedanimaltype() {    return fish.class; // fish extends animal } 

you can change return type class class<? extends animal>. following code allow return cat.class , dog.class, won't allow return integer.class :

abstract public class<? extends animal> getproducedanimaltype(); 

the client can write following code , compile fine:

public class<? extends animal> getproducedanimaltype()  {    return fish.class; // fish extends animal } 

but following code won't compile:

public class getproducedanimaltype()  {    return integer.class;  } 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -