I have a baseclass, Statement
, which several other classes inherit from, named IfStatement
, WhereStatement
, etc... What is the best way to perform a test in an if
statement to determine which sort of Statement
class an instance is derived from?
A Method provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method).
The main difference between Class and Method is that Class is a blueprint or a template to create objects while a method is a function that describes the behavior of an object.
Class methods are methods that are called on the class itself, not on a specific object instance. The static modifier ensures implementation is the same across all class instances. Many standard built-in classes in Java (for example, Math) come with static methods (for example, Math.
write a main method on your class, and call your test method. To check by running just write a main method and call this method with arguments. If you want to have a test case, take a look at JUnit or Mokito to write a test. There should be a way to run parts or the code without writing a main method or a test-class.
if(object instanceof WhereStatement) { WhereStatement where = (WhereStatement) object; doSomething(where); }
Note that code like this usually means that your base class is missing a polymorphic method. i.e. doSomething()
should be a method of Statement
, possibly abstract, that is overridden by sub-classes.
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