How to tell an object’s class type
If you need to know exactly what the object type of a class is during runtime, you can achieve that using the getClass() method of an object. The getClass() is method of java.lang.Object, which means that every java class has inherited that method.
Another technique that you can use is the instanceof operator. When faced dealing with instances of objects that has deep inheritance hierarchies, your ability to tell which to which exact class it belongs to could be a valuable technique.
The instanceof operator returns true when obj1 is exactly an “instance of” Circle, not when it’s an instance of an class that is “a kind of” Shape. It is precise.
If you want to show appreciation for my efforts dear reader, you could buy me a tall hazel nut Americano ($2) via PayPal. Thanks
Leave a comment