抽象类和所有方法都是抽象的类有什么区别?
我想知道 Java 中的抽象类和具有所有方法抽象的类有什么区别?我的意思是,抽象类只是一个其方法自动获取抽象的类吗?
I wonder what is the difference in Java between an abstract class and a class that has all its methods abstract? I mean, is an abstract class just a class whose methods automatically get abstract?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
绝对不是。事实上,一个类可以是抽象的,而没有任何方法都是抽象的,尽管这种情况相对较少(请参阅下面 Mark 的评论以获取示例)。另一方面,如果一个类具有任何抽象方法,那么它必须声明为抽象的。
一般来说,抽象类的目的是提供一个具有一些非抽象行为的骨架,但其他部分仍需由子类填充。例如,这可以与模板方法模式一起使用。
Absolutely not. Indeed, a class can be abstract without any methods being abstract, although that's relatively rare (see Mark's comment below for an example). On the other hand, if a class has any abstract methods, then it must be declared abstract.
Generally speaking, the purpose of an abstract class is to provide a skeleton with some non-abstract behaviour, but other bits still to be filled in by subclasses. This can be used with the template method pattern, for example.
任何包含一个或多个抽象方法的类也必须声明为抽象的。要声明类抽象,只需在类声明开头的 class 关键字前面使用abstract 关键字即可。抽象类中不能有对象。也就是说,抽象类不能直接用new操作符实例化。这样的对象是没有用的,因为抽象类没有完全定义。此外,您不能声明抽象构造函数或抽象静态方法。抽象类的任何子类必须实现超类中的所有抽象方法,或者将其本身声明为抽象。
Any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration. There can be no objects of an abstract class. That is, an abstract class cannot be directly instantiated with the new operator. Such objects would be useless, because an abstract class is not fully defined. Also, you cannot declare abstract constructors, or abstract static methods. Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared abstract.
抽象类和接口之间的唯一区别是抽象类可以被继承,而接口则不能,因此接口没有任何构造函数,与抽象类相反。
The Only difference between abstract class and interface is that abstract class can be inherited and whereas interfaces can't, thus interfaces don't have any constructors conversely with an abstract class.
每当你在类中创建一个抽象方法时,你就在类名前显式地提到abstract关键字,就像这样
}
以下是Java中与抽象类相关的要点
--> 抽象类是无法实例化的类之一。
-->如果你想从子类(其他人)获取任何方法的实现,那么抽象方法可以在这个意义上使用。
--> 抽象类是不完整的,子类必须声明缺失的部分才能成为具体类(可以实例化对象的类),否则这些子类也成为抽象类。
--> 您可以通过“抽象类”来实现作为 OOP 主要支柱的抽象。
抽象隐藏了对象的不相关细节。
--> IS A 关系(继承)的抽象用途。
--> 抽象用于实现多态行为(OOP 的另一个主要支柱)
--> 抽象类不应该是私有的,也不应该包含私有方法。
--> 您扩展单个抽象类而不是多个,因为 Java 是支持单一继承的
-->抽象类必须包含 1 个或 1 个以上抽象方法
--> 如果任何类包含抽象方法,那么它应该显式声明抽象类,即使它包含具体方法。
-->构造函数和静态方法不能声明为抽象方法,因为构造函数不是继承的。
-->如果子类没有实现超类的抽象方法,那么它也成为抽象类。
--> 尝试实例化抽象类的对象是一个编译错误。
--> 抽象超类变量可以保存子具体对象的引用。
Whenever you make a abstract method in the class then you explicitly mention the abstract keyword before the class name, just like this
}
Here are the points related Abstract class in Java
-->Abstract class is the one of class that cannot be instantiated.
-->If you want to get implementation of any method from child class(other person) then abstract method can use in this sense.
-->Abstract class are incomplete ,subclass must declare missing piece to become concrete class(Class whose object can be instantiated ) , otherwise these subclass also become abstract class.
-->You can achieve abstraction that is main pillar of OOP through by "abstract classes".
Abstraction hide the irrelevant detail of an object.
-->Abstract use for IS A Relationship (Inheritance).
-->Abstraction use for to achieve Polymorphic behavior (Another main pillar of OOP)
-->abstract class should not be private and not contained private method.
-->You extends single abstract class not multiple because Java is Single supported Inheritance
--> Abstract class must contain 1 or more than 1 abstract method
-->If any class contain abstract method then it should explicitly declare abstract class even if it contain concrete method.
--> Constructor and static method cannot be declared as abstract because constructor are not inherited.
--> If child class have not implement the abstract method of super class then it become also abstract class.
-->Attempting to instantiate the object of abstract class is an Compilation error.
-->Abstract super class variable can hold the reference of child concrete object.
在其声明中包含abstract关键字的类称为抽象类。
如果您不想为所有抽象方法提供实现,那么可以使用适配器类的概念:
例子:
A class which contains the abstract keyword in its declaration is known as abstract class.
If you don't want to provide implementation for all abstract methods then there is a concept of adapter class:
Example: