1

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?

4
  • Do they (Object1/2/3) share same interface? Commented Oct 16, 2014 at 13:03
  • Where comes Object from? please show more code. Commented Oct 16, 2014 at 13:05
  • 1
    A more elegant way would be to use inheritance and virtual methods. Commented Oct 16, 2014 at 13:06
  • stackoverflow.com/questions/5579309/switch-instanceof Commented Oct 16, 2014 at 13:06

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

The problem is, I am not entitled to change Object1, Object2, etc. It is not possible, that they implement an interface. Is there another solution?
you can use java refections and create a class with methods derrived from the class name and run them with reflection
1

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(){...}
}

1 Comment

Hmmm, i doubt if Object is good choice for an interface name (even if it just an example) :-)
0

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();

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.