为什么我们不能在 Java 中实例化抽象类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21681094/
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
Why can't we instantiate an abstract class in Java?
提问by sandejai
I understand:
我明白:
- Since an abstract class is nothing on its own, e.g. vehicle, we want to create an object of an concrete implementation, like Car, Bike, etc.
- The constructor of an abstract class gets called during object chaining.
- We can never directly create an object of an abstract class, even if it contains a constructor and all methods are implemented.
- 由于抽象类本身没有任何东西,例如车辆,我们想要创建一个具体实现的对象,例如汽车、自行车等。
- 抽象类的构造函数在对象链接期间被调用。
- 我们永远不能直接创建抽象类的对象,即使它包含一个构造函数并且所有方法都已实现。
But from the compiler's perspective, why does Java enforce these rules?
但是从编译器的角度来看,为什么 Java 会强制执行这些规则?
采纳答案by Eli Iser
This is not a technical limitation, rather (as you have pointed out) a logical one. Java (and many other languages) enforce various rules not because they are impossible to break, but because this is an intentional part of the language.
这不是技术限制,而是(如您所指出的)合乎逻辑的限制。Java(和许多其他语言)强制执行各种规则并不是因为它们不可能被破坏,而是因为这是该语言有意的一部分。
回答by rocketboy
Because an Abstract Class is a skeleton structure(an incomplete construct if you may), hence the term Abstract.
因为抽象类是一个骨架结构(如果可能的话,这是一个不完整的构造),因此术语抽象。
abstract class Person(){
abstract void Speak();
}
Means every Person
must speak. That means every person should know how to speak (implement the speak()
). new Person()
cannot have that, so it is not allowed.
意味着每个人都Person
必须说话。这意味着每个人都应该知道如何说话(实现speak()
)。new Person()
不能有那个,所以是不允许的。
回答by Sunil Singh Bora
回答by sandejai
What I understand that Abstract classes may contain abstract (empty without implementation) methods. If we instantiate an object and call the empty method, It's not going to work and may cause problem, hence compiler forces this RULE. any further in-sighter ?
据我所知,抽象类可能包含抽象(没有实现的空)方法。如果我们实例化一个对象并调用空方法,它将无法工作并可能导致问题,因此编译器会强制执行此规则。任何进一步的洞察力?
回答by OldCurmudgeon
Well actually you can - but only if you implement any methods that have been declared abstract or left out.
实际上你可以 - 但前提是你实现了任何被声明为抽象或遗漏的方法。
/**
* A classic adaptor pattern.
*
* @param <P>
* @param <Q>
*/
public static interface Adapter<P, Q> {
public Q adapt(P p);
}
/**
* An I walks an iterator of one type but delivers items of a different type.
*
* Please fill in the `next()` method. Use an Adaptor for convenience.
*
* @param <S>
* @param <T>
*/
public abstract static class I<S, T> implements Iterator<T> {
protected final Iterator<S> it;
public I(Iterator<S> it) {
this.it = it;
}
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public void remove() {
it.remove();
}
}
/**
* Use an adaptor to transform one type into another.
*
* @param <S>
* @param <T>
*/
public static class IA<S, T> extends I<S, T> {
private final Adapter<S, T> adaptor;
public IA(Iterator<S> it, Adapter<S, T> adaptor) {
super(it);
this.adaptor = adaptor;
}
@Override
public T next() {
// Implement the abstract method on-the-fly.
return adaptor.adapt(it.next());
}
}
Added
添加
The IA
class instantiates an object of the I
abstract class and implements the next
method that was missing from the I
class. You are actually creating an object of an anonymous that implements the implied abstract method.
的IA
类实例化的对象I
的抽象类和实现了next
这是从丢失的方法I
的类。您实际上是在创建一个匿名对象,该对象实现了隐含的抽象方法。
回答by Taylor
rocketboy shows some mechanistic reasons, but there's a conceptual reason.
Rocketboy 显示了一些机械原因,但有一个概念上的原因。
An Abstract class represents an abstract concept. Take your vehicle example. You cannot build a vehicle that is not something more specific. You can have a set of vehicles, that could be made of 2004 corolla's and '98 ford escorts and 1984 cs36 (a kind of yacht), a mark 4 firefly class mid-range bulk transport(the one with the stabilizers), you can take any one of those individually and call them a vehicle but you cannot have something that is only a vehicle and not one of those or some other specific type of vehicle.
Abstract 类代表一个抽象概念。以您的车辆为例。你不能建造一辆不是更具体的车辆。你可以拥有一套车辆,可以是 2004 卡罗拉和 98 福特 escorts 和 1984 cs36(一种游艇),一个马克 4 萤火虫级中型散装运输(带稳定器的那个),你可以将其中任何一个单独称为车辆,但您不能拥有仅是车辆而不是其中之一或其他特定类型车辆的东西。
Abstract classes represent such abstract concepts as vehicle. Hence the idea of instantiating one is non-sensical because to actually instantiate it you need to know what you're instantiating.
抽象类表示诸如车辆之类的抽象概念。因此,实例化一个的想法是没有意义的,因为要实际实例化它,您需要知道要实例化的内容。
回答by Ankita P.
You can't instantiate abstract class because it's just to give a structure your class which is extending it.
你不能实例化抽象类,因为它只是为你的类提供一个扩展它的结构。
And if you want to initiate your class, then why you want to define it as abstract?
如果您想启动您的类,那么为什么要将其定义为abstract?
回答by kevin
An abstract class is not complete! The author marked it abstract to tell you that some implementation is missing in the code. The author has done some of the work, but you must fill in some bits yourself to get it working. The abstract
keyword ensures that no one would accidentally initiate this incomplete class.
抽象类是不完整的!作者将其标记为抽象以告诉您代码中缺少某些实现。作者已经完成了一些工作,但您必须自己填写一些内容才能使其工作。该abstract
关键字确保没有人会不小心启动这个不完整的类。
Think of repairing a car. Someone has removed the brake pads and is going to replace them in the next day. Now, to prevent someone accidentally driving this car(which has no brakes installed), the mechanic installs a lock on the steering wheel. It's a fail-safe measure.
想想修理汽车。有人已经拆下刹车片,并准备在第二天更换。现在,为了防止有人不小心驾驶这辆车(没有安装刹车),机械师在方向盘上安装了一个锁。这是一种防故障措施。
回答by Rudra
abstract it self tells : existing in thought or as an idea but not having a physical or concrete existence. In java term , abstract keyword definition , if abstract comes before a class name, then JDK tells to JVM about discard that class object initiation. It's correct, abstract class can have multiple constructor but only for constructor chaining.
抽象它自己告诉:存在于思想中或作为一个想法存在,但没有物理或具体的存在。在 Java 术语中,abstract 关键字定义,如果 abstract 出现在类名之前,则 JDK 告诉 JVM 丢弃该类对象的初始化。没错,抽象类可以有多个构造函数,但只能用于构造函数链接。
回答by Bharti Rawat
Because Java restricted it that's why we can not instantiated the abstract class. Because in general scenario abstract means incomplete so we can not make of object of incomplete things.We have to provide the complete implementation of an abstract class in a concrete class. But we cannot create the object of an abstract class.
因为 Java 限制了它,所以我们不能实例化抽象类。因为在一般情况下抽象意味着不完整,所以我们不能把不完整的东西做成对象。我们必须在具体类中提供抽象类的完整实现。但是我们不能创建抽象类的对象。