Java 使用没有任何抽象方法的抽象类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18029919/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:53:31  来源:igfitidea点击:

Use of an abstract class without any abstract methods

javaabstract-classabstract

提问by gifpif

An abstract class need not include any abstract methods.

抽象类不需要包含任何抽象方法。

Is there any other reason to make a class abstract other than the fact that abstract classes can't be instantiated?

除了无法实例化抽象类这一事实之外,还有什么其他理由可以使类抽象?

采纳答案by Ushani

The principal role of an abstract class is to provide an appropriate root class from which concrete, (i.e. non-abstract) subclasses can be derived. This is a powerful and versatile feature which promotes code re-use. Abstract classes encapsulate general features that are common to a range of data types - features which are too general to be meaningful in the abstract class, but which can be overridden in a subclass

抽象类的主要作用是提供一个适当的根类,从中可以派生出具体的(即非抽象的)子类。这是一个强大且通用的功能,可促进代码重用。抽象类封装了一系列数据类型共有的通用特性——这些特性在抽象类中太通用而没有意义,但可以在子类中覆盖

Any class with an abstract method is automatically abstract itself and must define itself as such with the keyword abstract - interestingly, an abstract class need not contain any abstract methods

任何具有抽象方法的类都会自动抽象自己,并且必须使用关键字 abstract 定义自己——有趣的是,抽象类不需要包含任何抽象方法

An abstract class cannot be instantiated - in other words you cannot create instances (objects) of an abstract class

抽象类不能被实例化——换句话说,你不能创建抽象类的实例(对象)

References to objects of an abstract class can be declared even though objects of abstract classes cannot be instantiated, e.g Account a ; will not generate a syntax error

即使不能实例化抽象类的对象,也可以声明对抽象类的对象的引用,例如 Account a ;不会产生语法错误

If a subclass of an abstract class overrides, i.e. provides an implementation of every abstract method in its superclass, the subclass is called a concrete class and objects of the subclass can be created

如果抽象类的子类覆盖,即提供其超类中每个抽象方法的实现,则子类称为具体类,并且可以创建子类的对象

If a subclass of an abstract class does not override (implement) all of the abstract methods it inherits, that subclass itself is also abstract and must be declared as such

如果抽象类的子类没有覆盖(实现)它继承的所有抽象方法,则该子类本身也是抽象的,必须如此声明

回答by Prabhaker A

You can make a class as abstract,even if you don't want to implement all of the interface methods that the class implements. According to java docs.

您可以将类设为抽象类,即使您不想实现该类实现的所有接口方法。根据 java文档

It was noted that a class that implements an interface must implement all of the interface's methods. It is possible, however, to define a class that does not implement all of the interface methods, provided that the class is declared to be abstract.

注意到实现接口的类必须实现接口的所有方法。但是,可以定义不实现所有接口方法的类,前提是该类声明为抽象类。

Any way you can't instantiate a class that declared with abstract.

无论如何你都不能实例化一个用abstract声明的类。

回答by Thomas W

Probably not. It would prevent a "static helper" class from being instantiated, but I think using it that way would be poor style.

可能不是。它会阻止实例化“静态助手”类,但我认为以这种方式使用它会很糟糕。

As others say, if it's intended to be a base class -- with descendants declaring & implementing interfaces/ functionality, which is too specific for the superclass -- then abstractcould be appropriate in the superclass.

正如其他人所说,如果它打算成为一个基类——后代声明和实现接口/功能,这对于超类来说太具体了——那么在超类中abstract可能是合适的。

In that case it's a declaration that the super is not useful on it's own, and that subclasses will do something more -- which they will specifically declare.

在这种情况下,它声明了 super 本身没有用处,并且子类将做更多的事情——他们将特别声明。

回答by Ankur Shanbhag

Abstract class means the definition of the class is not completeand hence cannot be instantiated. Even though it does not have abstract method, it is an indicator that the class is available for inheritance. Even though it has implementation for all the methods in it, the implementation may still not be complete and must be overridden by the extending class.

抽象类意味着类的定义不完整,因此无法实例化。即使它没有抽象方法,它也表明该类可用于继承。即使它对其中的所有方法都有实现,该实现可能仍然不完整,必须由扩展类覆盖。

回答by Aniket Thakur

There cannot be an abstract function in an non abstract class!

非抽象类中不能有抽象函数!

So if you need an abstract function in your class for whatever reason you must declare your class to be abstract.

因此,如果出于某种原因需要在类中使用抽象函数,则必须将类声明为抽象类。

Why would I need an abstract function?

为什么我需要一个抽象函数?

What is the point of using abstract methods?

使用抽象方法有什么意义?

If an abstract class may not include any abstract method. Than is there any other reason to make class abstract except of abstract class can't be instantiated

如果一个抽象类可能不包含任何抽象方法。除了无法实例化抽象类之外,还有什么其他原因可以使类抽象

As for your question a scenario I can think of is as follows -

至于你的问题,我能想到的一个场景如下——

Lets say all you need is static functionality of a class. Static variables and function. So basically you don't want users to create any instance of your class. Now you can make your constructor private thereby ruling out the possibility of creating it's instance but you don't want to do that either. Why write additional lines of code? In this case you can make your class abstract.

假设您只需要一个类的静态功能。静态变量和函数。所以基本上你不希望用户创建你的类的任何实例。现在,您可以将构造函数设为私有,从而排除创建它的实例的可能性,但您也不想这样做。为什么要编写额外的代码行?在这种情况下,您可以使您的类抽象。

回答by Vikas V

I have extracted below points from Why should I declare a class as an abstract class?

我从为什么我应该将一个类声明为抽象类中提取了以下几点

  1. Abstract classes improve the situation by preventing a developer from instantiating the base class, because a developer has marked it as having missing functionality.

  2. It also provides compile-time safety so that you can ensure that any classes that extend your abstract class provide the bare minimum functionality to work, and you don't need to worry about putting stub methods that inheritors somehow have to magically know that they have to override a method in order to make it work.

  1. 抽象类通过阻止开发人员实例化基类来改善这种情况,因为开发人员已将其标记为缺少功能。

  2. 它还提供编译时安全性,以便您可以确保扩展抽象类的任何类都提供最低限度的工作功能,并且您无需担心放置存根方法,继承者必须以某种方式神奇地知道他们拥有覆盖一个方法以使其工作。

回答by Rravi

Abstract classes serve as templates for their subclasses. For example, the abstract class Tree and subclass, Banyan_Tree, has all the characteristics of a tree as well as characteristics that are specific to the banyan tree.

抽象类充当其子类的模板。例如,抽象类 Tree 和子类 Banyan_Tree 具有树的所有特征以及特定于榕树的特征。

Understanding the differences between an abstract class and an interface is essential. An interface only has method declarations or abstract methods and constant data members, while an abstract class may have abstract methods, member variables and concrete methods. Because Java only supports single inheritance, a class can implement several interfaces but can extend only one abstract class.

理解抽象类和接口之间的区别至关重要。一个接口只有方法声明或抽象方法和常量数据成员,而抽象类可能有抽象方法、成员变量和具体方法。因为Java只支持单继承,一个类可以实现多个接口,但只能扩展一个抽象类。