According to this Can an abstract class have a constructor? the constructor does not build the object and it is used to initialize fields so what is used to build the object? As in abstract class the constructor exists and it's not allowed to make an instance from that class and only could inherit it.
so the question is
What really builds the object in java ??
An instance of an object in Java is created every time you use the new keyword. For example,
MyClass myObject1 = new MyClass("John", 1, 1000.20);
MyClass myObject2 = new MyClass("Mary", 2, 200);
MyClass myObject3 = new MyClass();
However, you can only instantiate a concrete class and never a abstract class. So a question arises, what's the point in having an abstract class?
You have a abstract class to provide a common set of functionalities which can be shared by all its subclasses , eg, attributes, fields, etc.
And of course, an abstract can have a constructor. If you don't provide a constructor explicitly, it will have a default constructor . Whenever one of its concrete subclasses is instantiated, the constructor of the subclass will call the super constructor before doing any type of work in its own constructor. For example,
public abstract class MyAbstractClass {
private String name;
private int age;
public MyAbstractClass() {
name = "bozo";
age = 5;
}
public MyAbstractClass(String name, int age) {
this.name = name;
this.age = age;
}
...
}
public class MyClass extends MyAbstractClass {
private double income;
public MyClass() {
//calls super() implicitly.
}
public MyClass(String name, int age, double income) {
//calls super explicitly
super(name, age);
this.income = income;
}
}
Constructor is mandatory for any class.
All classes (abstract like concrete) have to declare a constructor.
It may be a generated no arg constructor or a explicitly declared constructor but anyway, the constructor presence is mandatory.
Why ?
Because for any class that inherits from a class (that is : all classes but Object
), the first statement of any invoked constructor is invoking the direct parent constructor, that may at its turn invoke its parent constructor, and so on.
Use the new
operator to create an instance
As you want to create an instance of a specific class, you have to invoke the constructor of its class by prefixing it with the new
operator :
new MyObject();
But it makes sense only for concrete classes.
Indeed, abstract classes cannot be instantiated
So, the constructor of the abstract class will be used but only as you create an instance of a child class (whatever its level in the hierarchy) of this abstract class (what I explained in the first point).
Suppose Child
is concrete and Parent
is abstract, you have to invoke the parent constructor of Child
in the Child
constructor :
Child(){super();}
But you will never invoke new Parent()
as it is not legal to instantiate an abstract class.
The new Operator does build the Object indeed.
The reason why an abstract class always has a constructor (explicit or implicit) is because it is called from it's not abstract subclasses.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.