Java :Interface Method's Overriding

标签 java methods interface static overriding

package chap8;  
 interface Interfaces
 {
     void nMethod();    //   normal method of interface

     default void dMethod() // default method of interface
     { System.out.println("Default Method of Interface"); }

     static void sMethod()  // static method of interface
     { System.out.println(" static method of interface"); }

 } 
 class IClass implements Interfaces
 { 
   public void nMethod()
    { System.out.println("Normal Method of Interface in IClass ");}
    static void sMethod()
    { System.out.println("Does function overrided ?");}
    public void dMethod()
    { System.out.println("default Method of Interface in IClass ");}
 }
 class MainClass
 {
     public static void main(String args[])
     {
        IClass ob =new IClass();

        ob.nMethod();
        ob.sMethod(); // overrided method ??
        ob.dMethod();

        // calling static

         //Interfaces.sMethod //  via Interfaces
        // IClass.sMethod();   // via IClass       (why all these sMethod calling showing error)



     }
 } 

问题:a) 接口(interface)中声明的 sMethod 是否被 IClass 中存在的 sMethod 覆盖?

b) 为什么我无法通过 Interface 和 IClass 调用 sMethod?

感谢 fpr 的帮助!!!

最佳答案

请参阅 default methods 上的 Oracle 文档和静态方法。

扩展包含默认方法的接口(interface)

当您扩展包含默认方法的接口(interface)时,您可以执行以下操作:

  1. 完全不提及默认方法,这可以让您的扩展接口(interface)继承默认方法。
  2. 重新声明默认方法,使其变得抽象。
  3. 重新定义默认方法并覆盖它

关于您的疑问:

  1. 如果您将这些概念应用到您的示例中,您的 dMethod 就会在 IClass 中被覆盖
  2. IClass 中的 sMethod() 隐藏了 Interfaces 中的 sMethod()

关于Java :Interface Method's Overriding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36734407/

相关文章:

java - 有不少于吗?

java - Junit-如何测试参数的无效使用

javascript - 尝试在数组中查找序列长度的问题

typescript ,类内的接口(interface)?

java - 可终结对象的前期成本是多少?

java - 使用 GSON 解析器迭代数据集

java - 我正在尝试验证用户名和密码

字符串中的Python类

php - 将 PHP 接口(interface)导出到 Typescript 接口(interface),反之亦然?

java - 为什么为每个类创建接口(interface)而不是其实现