Question

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.

EDIT: 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.

:)

Was it helpful?

Solution

Two ways to do this:

  1. Use the instanceof operator.

  2. Call getClass() on the object (make sure to check for null first).

OTHER TIPS

if (o instanceof SalariedEmp)
    doSomethingWith((SalariedEmp)o);

You can use RTTI (Run-Time Type Identification) by using the instanceof operator or by calling .getClass() on the instance, but that's almost always an indication that the super-type you're using is either the wrong super-type, or is badly designed.

In your case, you don't need to be using Object, as you already know that you at least have an Employee.

The Object Oriented paradigm is that you don't ask an object instance what it is, you ask it to do something. In this case, you have a choice of several thing you might want to ask an Employee object to do. Which one is the best choice for you depends on exactly what you're trying to model:

You could ask it if it is salaried, and then possibly cast it, by adding a virtual method boolean isSalaried() to the Employee base class. The base method would return false, but a SalariedEmployee would override that method and return true.

But while that avoids (the likely more expensive) RTTI, it doesn't avoid the cast. You could add a method SalariedEmployee asSalariedEmployee(), which in the base class would return null and in a SalariedEmployee would return this. That gives you a "safe" cast, but you still need a test against null.

Or you could just add a Money getSalary() method to Employee. Now you don't have to do any casting, but you'd need to decide what the salary of a non-salaried employee is; that could be null, zero, or NaN (the special Not a Number value).

If you decided to return null, in some cases (e.g, adding values) you'd have to test against null, in others (e.g, passing the salary to .equals()) you wouldn't need to test against null, as any instance.equals(null) should return false by the specification for Object.equals().

If you return zero, you can add without testing against null, but equals might be a bit weird -- is it really true that two hourly workers, each of whom get no salary, have the same (nonexistent) salary? That depends on what you're modeling. If it is true, return zero.

If it's not true that two hourly workers have the same non-existent salary, return a NaN value. This is justa specific case of the "Null Object Pattern".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top