简体   繁体   English

Java抽象关键字和接口

[英]Java Abstract Keyword and Interfaces

The code snippet is as follows: 代码段如下:

    interface Demo {   
        void incrementCount();  
        int getCount();  
     }    

    class ChildDemo implements Demo {   
        public int count = 10; 

        private void incrementCount() {    
             count++;   
        }   

        public int getCount(){    
             return count;   
        }       

        public static void main(String[] args){    
             int res;    
             Demo  ob= new ChildDemo ();    
             res = ob.getCount();    
             System.out.println(res);   
        }  
    }

And, the output whhich I get is as follows: 而且,我得到的输出如下:

    Compilation Error:incrementCount() in ChildDemo cannot implement incrementCount() in Demo; attempting to assign weaker access privileges to the method.

I would like to clarify a few things: 我想澄清一些事情:
1. Why is it an error? 1.为什么这是一个错误? What is attempting to assign weaker access privileges? 什么是尝试分配较弱的访问权限?
2. Changing it to private - can the method incrementCount() can still perform its calculations? 2.将其更改为private - 方法incrementCount()仍然可以执行其计算吗?
3. What changes should be made to get the output as 3.应该对输出进行哪些更改

10


4. What changes should be made to made to get the output as: 4.应该进行哪些更改以获得输出:

11

Thanks in advance. 提前致谢。

One thing is that it is not allowed, per specifiction, but if you could give the method private access, then it is would no longer be visible, hence breaking the contract of the interface. 有一点是,根据具体情况,它是不允许的,但是如果你可以给方法private访问,那么它将不再可见,因此破坏了接口的契约。 In your example, you're never calling incrementCount() , so the value of your count variable will stay 10 . 在您的示例中,您永远不会调用incrementCount() ,因此count变量的值将保持为10

Because you are restricting the method access to private . 因为您要限制方法访问private

All methods in an interface are implicitly public, regardless if you declare it explicitly or not. 无论您是否明确声明,接口中的所有方法都是隐式公开的。

http://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html http://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html

The public access specifier indicates that the interface can be used by any class in any package. 公共访问说明符表示该接口可以被任何包中的任何类使用。 If you do not specify that the interface is public, your interface will be accessible only to classes defined in the same package as the interface. 如果未指定接口是公共接口,则只能在与接口相同的程序包中定义的类上访问您的接口。

methods declared inside interface are by implicitly public and all varibales declared in the interface are implicity public static final(constants). 在接口内声明的方法是隐式public ,接口中声明的所有varibales都是隐含的public static final(常量)。

But, you are using private access 但是,您正在使用private

private void incrementCount(){    
    count++;   
} 

So the fix would be to add the public keyword to the method 因此,修复方法是将public关键字添加到方法中

public void incrementCount(){    
count++;   
} 

All the methods declared in an interface are by default public and abstract. 接口中声明的所有方法默认为public和abstract。 So you need to define these methods in the implementing class or you must declare the class as abstract. 因此,您需要在实现类中定义这些方法,或者必须将该类声明为抽象类。

You must understand the rules for overriding methods to get rid of this error. 您必须了解重写方法的规则以消除此错误。 One of the rule says that overridden methods cannot have weaker access specifier . 其中一条规则表明, 重写的方法不能具有较弱的访问说明符 So you cannot make a public methods as private or protected in the overridden method. 因此,您不能在重写方法中将公共方法设置为私有或受保护。 Make your Child class overridden method public and your code should work fine. 使您的Child类重写方法公开 ,您的代码应该正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM