小编Ank*_*jed的帖子

可以在子类中重写超类中的私有方法吗?

可以在Java中覆盖私有方法吗?如果不是,那么以下代码如何工作?

class Base{
      private void func(){
            System.out.println("In Base Class func method !!");         
      };
}

class Derived extends Base{
      public void func(){   //  Is this a Method Overriding..????        
            System.out.println("In Derived Class func method"); 
      }      
}

class InheritDemo{
      public static void main(String [] args){                      
            Derived d = new Derived();
            d.func(); 
      }
}
Run Code Online (Sandbox Code Playgroud)

java inheritance

26
推荐指数
3
解决办法
3万
查看次数

接口的成员变量必须是最终的......为什么?

我在脑海里有一个问题,为什么不能将接口中的成员变量变成非常数..静态的逻辑在我的脑海中是正确的,如果需要访问接口的变量那么它必须为它是静态的,因为我们无法创建接口的实例,但为什么需要最终产生?下面的代码显示了接口成员变量如何成为静态final,即使我们默认不提它....

interface inter{

       int a=10; // It becomes final static by default

       public void interFunc();
} 

class cls implements inter{

       public void interFunc(){

           System.out.println("In Class Method WITH a's Value as --> "+a);
       }
}

class Test{

       public static void main(String[] args){

           inter in= new cls();

           in.interFunc();      
           }
}
Run Code Online (Sandbox Code Playgroud)

提前致谢 !!!

java static final interface

7
推荐指数
2
解决办法
2万
查看次数

标签 统计

java ×2

final ×1

inheritance ×1

interface ×1

static ×1