Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在接口和抽象类Java中访问同名值_Java_Interface_Abstract Class_Parent Child - Fatal编程技术网

在接口和抽象类Java中访问同名值

在接口和抽象类Java中访问同名值,java,interface,abstract-class,parent-child,Java,Interface,Abstract Class,Parent Child,我有这样的抽象类和接口 abstract class ParentClass { int VALUE; public abstract void display(); public void display2() { System.out.println("this is abstract class method"); } } interface parentInterface { int VALUE=88; abstr

我有这样的抽象类和接口

abstract class ParentClass
{
    int VALUE;
    public abstract void display();
    public void display2()
    {
        System.out.println("this is abstract class method");
    }
}

interface parentInterface
{
    int VALUE=88;
    abstract void display();
    void display2();
}
子类扩展并实现了上述功能,如下所示

class ChildClass extends ParentClass implements parentInterface
{

    ChildClass()
    {
        super.VALUE=0;
        //VALUE=0;     //<=will give ambiguous property
    }
    @Override
    public void display() 
    {
        System.out.println("Current Class-"+this.getClass());
        System.out.println("Integer value-"+super.VALUE);
    }
    public void display2()
    {
        //to call the method of abstract class 
        //call by using super.display2();
        System.out.println("this is implemented method");
    }
}
class ChildClass扩展ParentClass实现parentInterface
{
儿童班()
{
super.VALUE=0;

//VALUE=0;//您可以使用
parentInterface从接口访问
。值
作为接口中的变量是
公共静态final


抽象类的
值可以使用
this.VALUE
访问,因为它是一个成员变量。

您可以使用
parentInterface从接口访问
值。值
作为接口中的变量是
公共静态final


抽象类的
值可以使用
this.VALUE
访问,因为它是一个成员变量。

接口中的变量隐式地
公共静态final

静态
-因为接口不能有任何实例

final
-分配给接口变量的值是不能由程序代码重新分配的真常量

接口变量可以使用
.VALUE
访问,而父类中的变量是继承的,因此可以使用
此.VALUE
访问

如果任何子类正在实现具有实例的接口 成员,如果子类和接口都在同一个包中 然后,可以从子类访问静态成员,而无需 甚至使用接口名


这就是为什么会出现不明确的错误。请将接口放在其他包中,然后它不应该显示这样的错误,否则您必须像
super那样访问它。值

接口中的变量隐式
公共静态final

静态
-因为接口不能有任何实例

final
-分配给接口变量的值是不能由程序代码重新分配的真常量

接口变量可以使用
.VALUE
访问,而父类中的变量是继承的,因此可以使用
此.VALUE
访问

如果任何子类正在实现具有实例的接口 成员,如果子类和接口都在同一个包中 然后,可以从子类访问静态成员,而无需 甚至使用接口名


这就是为什么会出现不明确的错误。请将接口放在其他包中,然后它就不应该显示这样的错误,否则您必须像
super那样访问它。VALUE

this.VALUE给出了不明确的this.VALUE给出了不明确的this.VALUE给出了不明确的this.VALUE给出了不明确的this.VALUE给出了不明确的this.VALUE给出了不明确的this.VALUE给出了不明确的this.VALUE