My method takes as input an Object. How do i determine it’s type, then cast it accordingly?
So for example:
binarySearch( Object o );
Inside the binarySearch method, i need a way to determine the type (or class) of Object o. Then i need to cast it with that type. how would i do that???
And more specifically, Object o is a child of a base class (EG SalariedEmp is child of Employee), and i specifically need the base class.
I figured out how to do what I wanted, which really should be a completely different question. I needed to use the .compareTo method from
Comparable to set up a way to compare two objects based on private data from those objects. All i had to do was implement my class as ‘Comparable’ using the
implements keyword. So no matter what Type of objects are compared in my binarySearch method, it works if they are of the same (unknown) type and have Comparable implemented. This makes my binarySearch method very re-usable with any type of comparable class.
So, in all fairness, I’ll accept the answer that covers the original question.
Two ways to do this:
getClass()on the object (make sure to check for null first).