Java cast interface to class

后端 未结 3 1464
花落未央
花落未央 2020-12-31 20:36

I have a question about interface and class implementing interface.

This is my code:

interface iMyInterface {
    public iMethod1();
}

public class          


        
3条回答
  •  盖世英雄少女心
    2020-12-31 20:57

    It will work (provided that cMyClass implements iMyInterface and you are in scope of protected modifier) but that is not the correct OO approch.

    If you want to use iMethod2 consider:

    • adding it to the interface
    • create another interface containing that method
    • Use cMyClass myClass = new cMyClass();

提交回复
热议问题