i have 2 interfaces inter1 and inter2 and class that implements both of them:
public interface Interface1 {
method1();
}
public interface Interface2 {
method2();
}
public class Implementer implements Interface1, Interface2 {
method1() {
// something
}
method2() {
// something
}
}
public class Test {
public static void main(String[] args) {
Interface1 obj = quest();
obj.method1();
if(obj instanceof Interface2) {
obj.method2(); //exception
}
}
public static Interface1 quest() {
return new cl();
}
}
How to cast obj to Interface2 and call method2() or it is possible to call method2() without casting ?
obj.method2(); //exception- it's not an exception: that's something that happens at runtime. This would be a compiler error.