I have code I'm working on to instantiate a CRC algorithm dependent on a polynomial passed in, and a string s
that contains "crc8" or "crc16" or "crc32".
The classes CRC8
, CRC16
, and CRC32
all extend a class CRC
and implement an interface HashAlgorithm
. Each of them has a constructor CRCx(int polynomial)
.
My problem is, I get this error on all 3 of the getConstructor() lines:
Type mismatch:
cannot convert from Constructor<HashFactory.CRC16>
to Constructor<HashFactory.CRC>
Can anyone help explain why and help me fix this?
int polynomial; // assign from somewhere
Constructor<CRC> crc = null;
if ("crc8".equals(s))
{
crc = CRC8.class.getConstructor(Integer.TYPE);
}
if ("crc16".equals(s))
{
crc = CRC16.class.getConstructor(Integer.TYPE);
}
if ("crc32".equals(s))
{
crc = CRC32.class.getConstructor(Integer.TYPE);
}
if (crc != null)
{
CRC crcInstance = crc.newInstance(polynomial);
return (HashAlgorithm) crcInstance;
}