Can a class which extends an abstract class be non abstract if it does not implement the methods of the abstract class it extends?

huangapple go评论65阅读模式
英文:

Can a class which extends an abstract class be non abstract if it does not implement the methods of the abstract class it extends?

问题

我已经很多年没有用Java编程了,以前也没有好好复习过这个概念。最近,我在某个地方读到,如果一个类实现了一个抽象类但没有实现其中的任何一个方法,那么这个类必须是抽象的。这是真的吗?还是有其他意思?我自己没有尝试过,因为我当前的电脑出了点问题,而且在线编译器在加载时卡住了。所以这是真的吗?提前谢谢。

英文:

I haven't coded in Java for years now and before too didn't revise this concept properly. Recently, I read somewhere that if a class that implements a abstract class but does not implement a single method from it has to be abstract. Is it true or does it mean something else? I have not tried this out myself because currently my PC is not working and the online compiler is getting stuck on loading. So is it true? Thanks in advance.

答案1

得分: 2

根据 Oracle 文档 https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html#:~:text=An%20abstract%20class%20is%20a,...%20also%20be%20declared%20abstract%20.

"当一个抽象类被子类化时,子类通常会为父类中的所有抽象方法提供实现。然而,如果没有提供这些实现,那么子类必须也被声明为抽象类。"

如果我们尝试执行以下操作:

abstract class A {
  abstract void draw();
  abstract void paint();
}

class B extends A {
  public void draw() {
    System.out.print("B实现了A的draw方法");
  }

  public void paint() {
    System.out.print("B实现了A的paint方法");

  }
}

class C extends A {
  public void draw() {
    System.out.print("C实现了A的draw方法");
  }
}

public class Main {

  public static void main(String[] args) {
    System.out.println("Hello world!");
    B b = new B();
    C c = new C();

    b.draw();
    b.paint();
    c.draw();
    c.paint();
  }
}

然后我们会得到如下编译错误:C不是抽象的,并且没有在A中覆盖抽象方法paint()

然而,如果我们将C定义为抽象类,并创建一个简单地扩展C的具体类D,如下所示:

abstract class C extends A {
  public void draw() {
    System.out.print("C实现了A的draw方法");
  }
}

class D extends C {
   public void paint() {
      System.out.print("D实现了A的paint方法");
   }
}

当然,还要更新我们的主方法,现在我们实例化的是类D而不是C(因为我们不能实例化抽象类),然后我们就能够成功编译和运行。所以,作为答案,是的,如果一个类extends了一个抽象类,但没有实现所有的方法,那么它也必须是抽象的。我希望我能够回答你的问题;我主要了解.NET,而且我在Java方面的知识有些陈旧。

英文:

Per the oracle docs https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html#:~:text=An%20abstract%20class%20is%20a,may%20not%20include%20abstract%20methods.&text=When%20an%20abstract%20class%20is,must%20also%20be%20declared%20abstract%20.

"When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract."

if we were to try to do the following:

abstract class A {
  abstract void draw();
  abstract void paint();
}

class B extends A {
  public void draw() {
    System.out.print("B implements A draw");
  }

  public void paint() {
    Systemout.print("B implements A paint");

  }
}

class C extends A {
  public void draw() {
    System.out.print("B implements A draw");
  }
}

public class Main {

  public static void main(String[] args) {
    System.out.println("Hello world!");
    B b = new B();
    C c = new C();

    b.draw();
    b.paint();
    c.draw();
    c.paint();
  }
}

Then we would get the following compiler error C is not abstract and does not override abstract method paint() in A

However, if we define C as an abstract class and then create a concrete class D that simply extends C as follows:

abstract class C extends A {
  public void draw() {
    System.out.print("C implements A draw");
  }
}

class D extends C {
   public void paint() {
      System.out.print("D implements A paint");
   }
}

and of course update our main method to where we now instantiate class D instead of C (since we cannot instantiate an abstract class) then we get a successful compile and run time. So then, as an answer, yes, if you have a class that extends an abstract class, but does not implement all of the method then it has to be abstract as well. I hope I was able to answer your question; my experience is mostly in .NET and I've grown rather stale in Java

答案2

得分: 2

不。

规则如下:

  1. 如果一个类包含任何抽象方法(无论是通过扩展抽象类还是具有抽象方法本身),则必须将其声明为抽象类。
  2. 如果一个类被声明为抽象类,则无论其是否实现所有抽象方法,都被视为抽象类。
英文:

No.

Rules are:

  1. If a class contains any abstract methods (either by extending an abstract class or by having abstract methods itself), then it must be declared as abstract.
  2. If a class is declared as abstract, then it is considered to be abstract regardless of the fact if it implements all abstract methods or not.

答案3

得分: 1

当然可以。抽象类A没有任何方法,子类B继承了A并且没有被声明为抽象。

abstract class A {
    //空
}

class B extends A {
    public void draw() {
        System.out.print("B实现了A的draw方法");
    }
}

public class Main {
    public static void main(String[] args) {
        B b = new B();
        b.draw();
    }
}
英文:

Ofcourse you can. abstract class A has not any methods,subclass B extends A and not be declared abstract.

abstract class A {
//empty
}

class B extends A {
    public void draw() {
        System.out.print("B implements A draw");
    }
}

public class Main {
    public static void main(String[] args) {
        B b = new B();
        b.draw();
    }
}

答案4

得分: 0

抽象类 Class1 {
    int val = 0;
    抽象 公共 整数 doit();
    
    公共 整数 done() {
        返回 val;
    }
}

以下代码无法编译。所有抽象方法都必须被实现。

类 Child1 扩展自 Class1 {
}

以下代码可以编译。继承的抽象方法和子类中的抽象方法不需要被实现。

抽象类 Child2 扩展自 Class1 {
}

您可以在 Java 语言规范 中了解更多关于抽象类的信息。


<details>
<summary>英文:</summary>


abstract class Class1 {
int val = 0;
abstract public int doit();

public int done() {
	   return val;
}

}

The following will not compile.  All abstract methods must be implemented.

class Child1 extends Class1 {
}

The following will compile. Inherited abstract methods and those in the subclass do not have to be implemented.

abstract class Child2 extends Class1 {
}

You can read more about [Abstract classes](https://docs.oracle.com/javase/specs/jls/se14/html/jls-8.html#jls-8.1.1.1) in the Java Language Specification. 

</details>



huangapple
  • 本文由 发表于 2020年9月15日 08:51:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63893564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定