Hello, suppose that i have interface MyInterface and 2 classes A, B which implements MyInterface, i declared 2 objects MyInterface a = new A() , and MyInterfave b = new B(). when i trying to pass a to function - function doSomething(A a){} i am getting error.
This is my code:
public interface MyInterface {
}
public class A implements MyInterface{
}
public class B implements MyInterface{
}
public class Tester {
public static void main(String[] args){
MyInterface a = new A();
MyInterface b = new B();
test(b);
}
public static void test(A a){
System.out.println("A");
}
public static void test(B b){
System.out.println("B");
}
}
My problem that i am getting from some component interface which can be all sorts of classes and i need to write function for each class so one way is to get interface and to check which type is it. (instance of A)
I would like to know how others deal with this problem??
Thx