I'm trying to determine if a generic class object is an instance of an abstract class. So far I'm not having much luck. Below is the code I'm trying to use. AbstractActivity is the name of a parent class I extend some of my activities from.
public void startActivity(Intent intent)
{
ComponentName name = intent.getComponent();
if(name != null)
{
Class<?> cls = null;
try {
cls = Class.forName(name.getClassName());
if(cls.isInstance(AbstractActivity));
{
//do something
}
else
{
super.startActivity(intent);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
super.startActivity(intent);
}
The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface). It is also known as type comparison operator because it compares the instance with type. It returns either true or false.
An abstract class has at least one abstract method. An abstract method will not have any code in the base class; the code will be added in its derived classes.
Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.
The only way to access the non-static method of an abstract class is to extend it, implement the abstract methods in it (if any) and then using the subclass object you need to invoke the required methods.
I would try:
if(AbstractActivity.class.isAssignableFrom(cls)) {
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With