接口和继承。 (错误指出方法不是抽象的。我该如何纠正?)

问题描述 投票:-1回答:1

编写程序以创建接口,在该接口中您必须声明变量int x = 20,并在该类中创建一个抽象类,再次声明变量int x = 10,创建一个名为test的新类,该类继承该类中的抽象类和接口创建方法print并打印x的所有值。

interface A
{
    int x = 20;
    void abc(); 
}
abstract class C implements A
{
    void def();
    {
        int x = 10;
    }
}
class test extends C
{
    static void print()
    {
        test obj = new test();
        obj.abc();
        System.out.println(x);
        test obj1 = new test();
        obj.def();
        System.out.println(x);
    }
    public static void main(String arg[])
}
java inheritance methods interface extends
1个回答
0
投票
有2个问题:

首先,在编写def()方法时存在语法问题:

void def(); { int x = 10; }

您需要删除该;

void def() { int x = 10; }

[第二,类C实现接口A,因此C必须实现abc(),否则,则需要声明为抽象。您声明了C抽象,因此很好,不需要实现abc()

然后test扩展C并继承该方法(abc())。由于test未被声明为抽象,因此它不能具有未实现的方法,因此需要实现abc()

因此您需要在abc()类中实现test

public void abc() { // some code }

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.