cast an object to an interface in java?

后端 未结 8 1694
半阙折子戏
半阙折子戏 2021-02-01 23:36

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

相关标签:
8条回答
  • 2021-02-02 00:29

    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.

    0 讨论(0)
  • 2021-02-02 00:39

    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.

    0 讨论(0)
提交回复
热议问题