java java中的抽象类必须实现什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16974998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what must be implemented from an abstract class in java?
提问by Joshua Oliphant
I have two questions really. I'm trying to get a handle on how inheritance works.
我真的有两个问题。我正在尝试了解继承的工作方式。
If I have an abstract class to inherit from, and it has a method that is not labelled abstract does this method still need to be implemented in the subclass?
如果我有一个抽象类要继承,并且它有一个没有标记为抽象的方法,这个方法还需要在子类中实现吗?
If I have a subclass that is inheriting from another subclass, which is then inheriting from an abstract class, does the lowest subclass need to implement the methods in the abstract class? Or because the methods have been implemented in the middle subclass, they don't need to be implemented again?
如果我有一个从另一个子类继承的子类,然后从一个抽象类继承,那么最低的子类是否需要实现抽象类中的方法?还是因为方法已经在中间子类中实现了,就不需要再实现了?
Thank you!
谢谢!
回答by Wils
An abstract class is a class that is declared abstract. It may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
抽象类是声明为抽象的类。它可能包含也可能不包含抽象方法。抽象类不能被实例化,但它们可以被子类化。
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
抽象方法是在没有实现的情况下声明的方法(没有大括号,后跟分号),如下所示:
abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, the class itself must be declared abstract, as in:
如果一个类包含抽象方法,则该类本身必须声明为抽象的,如下所示:
public abstract class GraphicObject {
// declare fields
// declare non-abstract methods
abstract void draw();
}
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, the subclass must also be declared abstract
当抽象类被子类化时,子类通常为其父类中的所有抽象方法提供实现。但是,如果不是,则子类也必须声明为抽象的
回答by arynaq
- If the method is not abstract it has been implemented already, when you subclass the abstract class you inherit the method implementation, re-implementing it would be overriding it. If the method was declared abstract you must implement or get compile-time error if the subclass is also not declared abstract.
- If you are inheriting from a class
A extends AbstractClass
that is not abstract then A must have implemented any abstract methods or again compileerror. If it hasn't implemented any abstract classes then A must also be abstract and responsibility of implementing the abstract methods fall on subclassers of A. Any sublcassers that do not implement the method must also be declared abstract untill finally a subclass implements it.
- 如果方法不是抽象的,它已经被实现了,当你继承抽象类的子类时,你继承了方法的实现,重新实现它会覆盖它。如果该方法被声明为抽象的,如果子类也未声明为抽象的,则您必须实现或得到编译时错误。
- 如果您从一个
A extends AbstractClass
非抽象类继承,那么 A 必须实现了任何抽象方法或再次编译错误。如果它没有实现任何抽象类,那么 A 也必须是抽象的,并且实现抽象方法的责任落在 A 的子类上。任何没有实现该方法的子类也必须声明为抽象,直到子类最终实现它。