Is the JVM an internal object instance for an abstract class?
I have an abstract class and a concrete subclass of it, when I create a subclass object, it automatically calls the super constructor. Is the JVM internally creating an object of an abstract class?
public abstract class MyAbstractClass {
public MyAbstractClass() {
System.out.println("abstract default constructor");
}
}
public class ConcreteClass extends MyAbstractClass{
public static void main(String[] args) {
new ConcreteClass();
}
}
then how does a constructor exist without an object in the JVM ?? (In case of abstract class)
Also the constructor is run after the object is created without creating an object of the abstract class, how is the default constructor executed? (This is mentioned in the Java Doc )
source to share
Is it true that you cannot create an abstract class?
Yes.
Is the JVM internally creating an object of an abstract class?
No, but this is a common misunderstanding (and it wouldn't be an unreasonable way to do this, as these are prototypal languages ββlike JavaScript).
The JVM creates an object one that belongs to the class you created (in your case ConcreteClass
). There are aspects of this one object that it gets from its superclass ( MyAbstractClass
) and from its subclass ( ConcreteClass
), but there is only one object.
An object is a collection of all its parts, including parts that seem to have the same name, such as a superclass method that is overridden by a subclass. In fact, these methods have different fully qualified names and do not conflict with each other, so it is possible to call the superclass version of the overridden method.
So, if it's just one object, why are you seeing a constructor call MyAbstractClass
? Before we answer that, I have to mention a couple of things the Java compiler does that you don't see in the source code:
-
Creates a default constructor for
ConcreteClass
. -
In this constructor, it calls the constructor
MyAbstractClass
. -
Just to be careful: in the constructor,
MyAbstractClass
it adds a call to the superclass constructor (Object
), because there is nosuper(...)
call written inside the constructorMyAbstractClass
.
This is what the code looks like with the bits that the Java compiler adds for you:
public abstract class MyAbstractClass {
public MyAbstractClass() {
super(); // <== The Java compiler adds this call to Object constructor (#3 in the list above)
System.out.println("abstract default constructor");
}
}
public class ConcreteClass extends MyAbstractClass{
ConcreteClass() { // <== The Java compiler adds this default constuctor (#1 in the list above)
super(); // <== Which calls the superclass (MyAbstractClass's) constructor (#2 in the list above)
}
public static void main(String[] args) {
new ConcreteClass();
}
}
Okay, with that in mind, let's touch on the point of TheLostMind very useful in the comments: Constructors don't create , they initialize them. The JVM creates an object and then runs as many constructors as possible (they really should be called initializers) on a single object to give each superclass the ability to initialize its part of the object.
So in this code, what's going on (and you can walk through it in the debugger to fully understand it):
-
JVM creates object
-
The constructor
ConcreteClass
is called-
The first thing a constructor does is call its superclass constructor, in this case the constructor
MyAbstractClass
. (Note that this is an absolute requirement: the Java compiler will not allow you to have any logic in the constructor itself until the superclass constructor is called.)-
the first thing a constructor does is call its superclass constructor (
Object
) -
When the constructor
Object
returns, the rest of the constructorMyAbstractClass
runs
-
-
When the constructor
MyAbtractClass
returns, the remainder of the constructor isConcreteClass
run
-
-
The object is returned as the result of the expression
new ConcreteClass()
.
Note that the above would get complicated if there were instance fields with initializers. See the JLS and JVM specifications for details.
source to share