This question already has an answer here:
这个问题在这里已有答案:
- Can an abstract class have a constructor? 21 answers
- 抽象类可以有构造函数吗? 21个答案
Why does an abstract
class in Java have a constructor
?
为什么Java中的抽象类有构造函数?
What is it constructing, as we can't instantiate an abstract
class?
它构造的是什么,因为我们无法实例化一个抽象类?
Any thoughts?
有什么想法吗?
7 个解决方案
#1
74
A constructor in Java doesn't actually "build" the object, it is used to initialize fields.
Java中的构造函数实际上并不“构建”该对象,它用于初始化字段。
Imagine that your abstract class has fields x and y, and that you always want them to be initialized in a certain way, no matter what actual concrete subclass is eventually created. So you create a constructor and initialize these fields.
想象一下,你的抽象类有字段x和y,并且你总是希望它们以某种方式初始化,无论最终创建什么实际的具体子类。因此,您创建一个构造函数并初始化这些字段。
Now, if you have two different subclasses of your abstract class, when you instantiate them their constructors will be called, and then the parent constructor will be called and the fields will be initialized.
现在,如果您有两个不同的抽象类子类,当您实例化它们时,将调用它们的构造函数,然后将调用父构造函数并初始化字段。
If you don't do anything, the default constructor of the parent will be called. However, you can use the super keyword to invoke specific constructor on the parent class.
如果您不执行任何操作,将调用父级的默认构造函数。但是,您可以使用super关键字来调用父类的特定构造函数。
#2
10
All the classes including the abstract classes can have constructors.Abstract class constructors will be called when its concrete subclass will be instantiated
包括抽象类在内的所有类都可以有构造函数。当实例化其具体子类时,将调用抽象类构造函数
#3
8
Two reasons for this:
有两个原因:
1) Abstract classes have constructors
and those constructors are always invoked when a concrete subclass is instantiated. We know that when we are going to instantiate a class, we always use constructor of that class. Now every constructor invokes the constructor of its super class with an implicit call to super()
.
1)抽象类具有构造函数,并且在实例化具体子类时始终调用这些构造函数。我们知道当我们要实例化一个类时,我们总是使用该类的构造函数。现在,每个构造函数都调用super()的隐式调用来调用其超类的构造函数。
2) We know constructor are also used to initialize fields of a class. We also know that abstract classes may contain fields and sometimes they need to be initialized somehow by using constructor.
2)我们知道构造函数也用于初始化类的字段。我们也知道抽象类可能包含字段,有时需要使用构造函数以某种方式初始化它们。
#4
7
Because another class could extend it, and the child class needs to invoke a superclass constructor.
因为另一个类可以扩展它,并且子类需要调用超类构造函数。
#5
4
Because abstract classes have state (fields) and somethimes they need to be initialized somehow.
因为抽象类具有状态(字段)和某些时间,所以需要以某种方式初始化它们。
#6
2
I guess root of this question is that people believe that a call to a constructor creates the object. That is not the case. Java nowhere claims that a constructor call creates an object. It just does what we want constructor to do, like initialising some fields..that's all. So an abstract class's constructor being called doesn't mean that its object is created.
我猜这个问题的根源是人们认为对构造函数的调用会创建对象。事实并非如此。 Java没有声称构造函数调用创建了一个对象。它只是做我们想要的构造函数来做,比如初始化一些字段......就是这样。因此,调用抽象类的构造函数并不意味着它的对象是被创建的。
#7
1
Implementation wise you will often see inside super() statement in subclasses constructors, something like:
实现明智,你经常会在子类构造函数中看到super()语句内部,例如:
public class A extends AbstractB{
public A(...){
super(String constructorArgForB, ...);
...
}
}
#1
74
A constructor in Java doesn't actually "build" the object, it is used to initialize fields.
Java中的构造函数实际上并不“构建”该对象,它用于初始化字段。
Imagine that your abstract class has fields x and y, and that you always want them to be initialized in a certain way, no matter what actual concrete subclass is eventually created. So you create a constructor and initialize these fields.
想象一下,你的抽象类有字段x和y,并且你总是希望它们以某种方式初始化,无论最终创建什么实际的具体子类。因此,您创建一个构造函数并初始化这些字段。
Now, if you have two different subclasses of your abstract class, when you instantiate them their constructors will be called, and then the parent constructor will be called and the fields will be initialized.
现在,如果您有两个不同的抽象类子类,当您实例化它们时,将调用它们的构造函数,然后将调用父构造函数并初始化字段。
If you don't do anything, the default constructor of the parent will be called. However, you can use the super keyword to invoke specific constructor on the parent class.
如果您不执行任何操作,将调用父级的默认构造函数。但是,您可以使用super关键字来调用父类的特定构造函数。
#2
10
All the classes including the abstract classes can have constructors.Abstract class constructors will be called when its concrete subclass will be instantiated
包括抽象类在内的所有类都可以有构造函数。当实例化其具体子类时,将调用抽象类构造函数
#3
8
Two reasons for this:
有两个原因:
1) Abstract classes have constructors
and those constructors are always invoked when a concrete subclass is instantiated. We know that when we are going to instantiate a class, we always use constructor of that class. Now every constructor invokes the constructor of its super class with an implicit call to super()
.
1)抽象类具有构造函数,并且在实例化具体子类时始终调用这些构造函数。我们知道当我们要实例化一个类时,我们总是使用该类的构造函数。现在,每个构造函数都调用super()的隐式调用来调用其超类的构造函数。
2) We know constructor are also used to initialize fields of a class. We also know that abstract classes may contain fields and sometimes they need to be initialized somehow by using constructor.
2)我们知道构造函数也用于初始化类的字段。我们也知道抽象类可能包含字段,有时需要使用构造函数以某种方式初始化它们。
#4
7
Because another class could extend it, and the child class needs to invoke a superclass constructor.
因为另一个类可以扩展它,并且子类需要调用超类构造函数。
#5
4
Because abstract classes have state (fields) and somethimes they need to be initialized somehow.
因为抽象类具有状态(字段)和某些时间,所以需要以某种方式初始化它们。
#6
2
I guess root of this question is that people believe that a call to a constructor creates the object. That is not the case. Java nowhere claims that a constructor call creates an object. It just does what we want constructor to do, like initialising some fields..that's all. So an abstract class's constructor being called doesn't mean that its object is created.
我猜这个问题的根源是人们认为对构造函数的调用会创建对象。事实并非如此。 Java没有声称构造函数调用创建了一个对象。它只是做我们想要的构造函数来做,比如初始化一些字段......就是这样。因此,调用抽象类的构造函数并不意味着它的对象是被创建的。
#7
1
Implementation wise you will often see inside super() statement in subclasses constructors, something like:
实现明智,你经常会在子类构造函数中看到super()语句内部,例如:
public class A extends AbstractB{
public A(...){
super(String constructorArgForB, ...);
...
}
}