John
John

Reputation: 815

Multiple instanceof in a if statement

I have a if statement, with multiple instanceof checks. Example:

if (object instanceof Object1) {
    // do something
} else if (object instanceof Object2) {
    // to something else
} else if (object instanceof Object2) {
    // and something else
} ...

What would be a more elegant way to solve this if-else-query?

Upvotes: 1

Views: 6548

Answers (4)

slim
slim

Reputation: 41223

This feels like a missed opportunity for polymorphism.

This is where a bunch of classes share the same method signatures as their superclass/interface, so the code that calls it doesn't need to know which type it is.

Without polymorphism:

Employee employee = ...;
if(employee instanceof Doctor) {
     salary = calcDoctorSalary(...);
} else if(employee instanceof Nurse) {
     salary = calcNurseSalary(...);
}

With polymorphism:

Employee employee = ...;
salary = employee.calcSalary(...);

The magic goes into the subclasses. calcSalary() is abstract in a superclass, or a method signature in an interface:

public abstract class Employee {
     public abstract int calcSalary(...);
}

... or ...

public interface Employee {
    public int calcSalary(...);
}

Then the type-dependent logic goes into the subclasses:

public class Nurse implements Employee {
    @Override
    public int calcSalary(...) {
         // code specific to nurses goes here.
    }
}

Whether to extend a class, or implement an interface, is something you'll learn with experience. Often when one doesn't start with an interface, one regrets it later.

Upvotes: 0

euthimis87
euthimis87

Reputation: 1143

This seems to be polymorphism so you can create an Interface and implement it for every Object.

interface ObjectToBeImplemented{
 method();
}

class Object1 implements ObjectToBeImplemented{ 
 @Override
 method(){...}
}
class Object2 implements ObjectToBeImplemented{ 
 @Override
 method(){...}
}

class Object3 implements ObjectToBeImplemented{ 
 @Override
 method(){...}
}

Upvotes: 1

Dima
Dima

Reputation: 8652

the best practice in OOP is to put the logic in the object itself and make it implement an interface:

the interface:

public interface MyLogic{
    public void doLogic();
}

first object:

public class Object1 implements MyLogic{
  public void doLogic(){// logic 1 here}
}

second object:

public class Object2 implements MyLogic{
  public void doLogic(){// logic 2 here}
}

and now just move your logic to the objects itself and instead all the if statements just use

object.doLogic(); // make sure object is from type MyLogic, if not, cast it

Upvotes: 4

ehanoc
ehanoc

Reputation: 2217

This is a typical usage for interfaces. See interfaces as defining the Type of an instance. Therefore, you know that all instance of a certain Type can do a particular task. At that point, you don't really care about the concrete class of the object, you just know it has that particular interfaces available to you to use.

Example :

public interface Worker {
     public void doWork();
}

public class Object1 implements Worker {
    public void doWork() {
      // work for this specific object
    }
}

public class Object2 implements Worker {
    public void doWork() {
      // work for this specific object
    }
}

public class Object3 implements Worker {
    public void doWork() {
      // work for this specific object
    }
}

Then your if statements would be replaced by

obj.doWork();

Upvotes: 0

Related Questions