if we cast an object to an interface, won\'t this object be able to call its own methods? in the following example, myObj
will only be able to call MyInterface meth
There is no difference. You will only be able to invoke MyInterface
interface methods on myObj
and mySec
. Obviously your code will only compile if both Obj
and Sec
implement the Myinterface
interface.
Changing the type of a variable (i.e. a reference) does not change the object itself. An object can always call its own methods, no matter what the types of declared variables are that reference to it.