Suppose a class MyClass implements an interface MyInterface, and it has its own instance method let's say foo(). When i create an instance of MyClass like this:
MyInterface myClass = new MyClass();
The compiler wouldn't let me access its instance method without an explicit casting:
myClass.foo(); // Can't resolve symbol
((MyClass) myClass).foo(); // this is okay
Even though the compiler obviously knows myClass is an instance of MyClass:
if(myClass instanceof MyClass)
System.out.println(myClass.getClass().getName()); //this will print "MyClass"
Why do i need to use cast for compiler to allow me to access the instance method?