public boolean isUserControled(){
return action.getClass().getSuperclass().toString().equals("class logic.UserBehaviour");
}
I think this piece of code is pretty self-explanatory. Is there a smarter way to do this?
Thanks
To determine the superclass of a class, you invoke the getSuperclass method. This method returns a Class object representing the superclass, or returns null if the class has no superclass. To identify all ancestors of a class, call getSuperclass iteratively until it returns null.
Here, member can be either a method or an instance variable. Although the instance variable i in B hides the i in A, super allows access to the i defined in the superclass. super can also be used to call methods that are hidden by a ...
Use of super with methodsThis is used when we want to call the parent class method. So whenever a parent and child class have the same-named methods then to resolve ambiguity we use the super keyword. This code snippet helps to understand the said usage of the super keyword.
A: The Object class, which is stored in the java. lang package, is the ultimate superclass of all Java classes (except for Object ).
(action instanceof logic.UserBehaviour)
will return true if action is an object of a type that extends UserBehavior.
An excerpt from http://download.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
The Type Comparison Operator instanceof
The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
The following program, InstanceofDemo, defines a parent class (named Parent), a simple interface (named MyInterface), and a child class (named Child) that inherits from the parent and implements the interface.
class InstanceofDemo {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent));
System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child));
System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface));
System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent));
System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child));
System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface));
}
}
class Parent{}
class Child extends Parent implements MyInterface{}
interface MyInterface{}
Output:
obj1 instanceof Parent: true
obj1 instanceof Child: false
obj1 instanceof MyInterface: false
obj2 instanceof Parent: true
obj2 instanceof Child: true
obj2 instanceof MyInterface: true
When using the instanceof operator, keep in mind that null is not an instance of anything.
Unless you specifically want to check only the very first superclass, it would be better to use:
return (action instanceof logic.UserBehavior);
Your method would be better with this:
action.getClass().getSuperClass().name().equals("logic.UserBehavior");
The call to toString()
is not the best idea.
Or better yet, as posted by Ulrik:
action.getClass().getSuperClass() == logic.UserBehavior.class
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