Stack Exchange
Differentiate blocks of code based on class without instanceof
So I have the following inteface I which is implemented by classes A and B:
interface I { ... }
class A implements I { ... }
class B implements I { ... }

Then I have another interface OverrideMe whose method has an object I as parameter, and a class that overrides that method and behaves differently depending on the actual class of that parameter:
interface OverrideMe {
   void method(I parameter)
}

class Overriding implements OverrideMe {
   @Override
   void method(I parameter) {
      if (I instanceof A) {
         // Code block for A
      }
      else if (I instanceof B) {
         // Code block for B
      }
   }
}

Is there a way differentiate both blocks of code by type and without using instanceof? I am not sure if this could be done using inheritance.
Keep in mind that I have no control on I, A, B and , I have to use them as they are.

Why don't you want to do this?

If you want to call a method that both A and B implement, you don't need that differentiation, the dynamic method lookup will solve that problem. If you want to call a method that, for instance, only B implements, then of course you need to do it (but just for B).

I don't want to do this because apparently "...is better to avoid using the instanceof operator in a series of if/else statements to differentiate blocks of code based on type, if these types are related by inheritance, it is cleaner to just define a method in the base class, and use overridden methods in these classes...".

Yes you can depending on what you want to do inside the code block. If you are looking for operating some b-logic on the instance of A or B. You can provide a method declaration inside the interface I and provide the implementation inside class A and B. Example,
interface I { 
   void doSomething();
 }
class A implements I {
   void doSomething(){
    //do something here 
} 
 }
class B implements I { 

   void doSomething(){
    //do something here 
} 
 }

interface OverrideMe {
   void method(I parameter)
}

class Overriding implements OverrideMe {
   @Override
   void method(I parameter) {
       parameter.doSomething();
   }
}

If aforementioned is not the case, you might have to use instanceof

Thanks Raunak, your solution indeed works and makes sense, my problem is that I have no control over I, A or B, so I cannot add methods to them.
