java 抽象类和所有方法都是抽象的类有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3419084/
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 is the difference between an abstract class and a class that has all its methods abstract?
提问by Albus Dumbledore
I wonder what is the difference in Java between an abstractclass and a class that has all its methods abstract? I mean, is an abstractclass just a class whose methods automatically get abstract?
我不知道是什么样的之间在Java中的差异抽象类和具有它的所有方法的类抽象?我的意思是,抽象类只是一个方法自动获得抽象的类吗?
回答by Jon Skeet
Absolutely not. Indeed, a class can be abstract without anymethods being abstract, although that's relatively rare (see Mark's comment below for an example). On the other hand, if a class has anyabstract methods, then it mustbe declared abstract.
绝对不。事实上,一个类可以是抽象的,而没有任何抽象的方法,尽管这种情况相对较少(参见下面 Mark 的评论示例)。另一方面,如果一个类有任何抽象方法,那么它必须被声明为抽象的。
Generally speaking, the purpose of an abstract class is to provide a skeleton with somenon-abstract behaviour, but other bits still to be filled in by subclasses. This can be used with the template method pattern, for example.
一般来说,抽象类的目的是提供一个具有一些非抽象行为的骨架,但其他位仍由子类填充。例如,这可以与模板方法 pattern一起使用。
回答by Albus Dumbledore
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.
任何包含一个或多个抽象方法的类也必须声明为抽象的。要声明类抽象,只需在类声明开头的 class 关键字前使用 abstract 关键字。抽象类不能有对象。也就是说,抽象类不能直接用 new 操作符实例化。这样的对象将是无用的,因为抽象类没有完全定义。此外,您不能声明抽象构造函数或抽象静态方法。抽象类的任何子类必须要么实现超类中的所有抽象方法,要么本身被声明为抽象的。
回答by spitti84
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.
抽象类和接口之间的唯一区别是抽象类可以被继承而接口不能,因此与抽象类相反,接口没有任何构造函数。
回答by Yasir Shabbir Choudhary
Whenever you make a abstract method in the class then you explicitly mention the abstract keyword before the class name, just like this
每当你在类中创建一个抽象方法时,你就会在类名之前明确提到抽象关键字,就像这样
public abstract class Test {
abstract void show();
}
}
Here are the points related Abstract class in Java
下面是Java中与Abstract类相关的要点
-->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.
-->您可以通过“抽象类”实现作为OOP主要支柱的抽象。抽象隐藏了对象的不相关细节。
-->Abstract use for IS A Relationship (Inheritance).
--> IS A关系(继承)的抽象用法。
-->Abstraction use for to achieve Polymorphic behavior (Another main pillar of OOP)
-->抽象用于实现多态行为(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
-->您扩展了单个抽象类而不是多个,因为 Java 是单一支持的继承
--> Abstract class must contain 1 or more than 1 abstract method
--> 抽象类必须包含1个或1个以上的抽象方法
-->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.
-->抽象超类变量可以保存子具体对象的引用。
回答by pankaj
A class which contains the abstract keyword in its declaration is known as abstract class.
在其声明中包含抽象关键字的类称为抽象类。
- Abstract classes may or may not contain abstract methods ie., methods with out body ( public void get(); )
- But, if a class have at least one abstract method, then the class must be declared abstract.
- If a class is declared abstract ,it cannot be instantiated.
- To use an abstract class you have to inherit it from another class, provide implementations to the abstract methods in it.
- If you inherit an abstract class you have to provide implementations to all the abstract methods in it.
- 抽象类可能包含也可能不包含抽象方法,即没有主体的方法(public void get();)
- 但是,如果一个类至少有一个抽象方法,那么这个类必须被声明为抽象的。
- 如果一个类被声明为抽象的,它就不能被实例化。
- 要使用抽象类,您必须从另一个类继承它,并为其中的抽象方法提供实现。
- 如果您继承一个抽象类,则必须为其中的所有抽象方法提供实现。
If you don't want to provide implementation for all abstract methods then there is a conceptof adapter class: Example:
如果您不想为所有抽象方法提供实现,那么有一个概念的适配器类:例如:
abstract class A{
public void m1();
public void m2();
public void m3();
}
class B extends A{
public void m1(){}
public void m2(){}
public void m3(){}
}
class C extends B{
public void m2(){
System.out.println("Hello m2");
}
public static void main(String args){
C obj=new C();
C.m2();
}
}

