Can we instantiate an ABSTRACT CLASS
Also can we have constructors in abstract classes?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Can we instantiate an ABSTRACT CLASS. I believe the answer is NO. But why?
Also can we have constructors in abstract classes?
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
While I'm not the best person to argue semantics in this area, I would say that while the abstract class's instance fields are initialized and its constructor is called, it's not being instantiated.Originally posted by Roger Chung-Wee:
Although you (the programmer) cannot instantiate an abstract class, such a class will be instantiated when . . .
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
I understand what you mean, but I'm making the semantic argument that this is called initializing the superclass. The distinction is that when you use the new operator or its equivalents, you are instantiating a single object that has a single class type, whereas each class in the type's chain of ancestors is responsible for initializing a portion of the object's state.Originally posted by Roger Chung-Wee:
The abstract class has to be instantiated, otherwise its instance variables cannot be inherited by the subsclass. This would result in an incomplete subclass object being created.
Since each object is a single instance of a class, and a single object is created (ignoring any created by field and instance initializers or constructors) for a single instance creation expression, it follows that only one class is instantiated.We say that a class is instantiated when an instance of the class is created by a class instance creation expression. Class instantiation involves determining what class is to be instantiated, what the enclosing instances (if any) of the newly created instance are, what constructor should be invoked to create the new instance and what arguments should be passed to that constructor.
A subclass of an abstract class that is not itself abstract may be instantiated, resulting in the execution of a constructor for the abstract class and, therefore, the execution of the field initializers for instance variables of that class.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Now, all classes which extends this must supply a constructor which takes a String, and implement a validate method.
I agree that the abstract class's constructor is called, and its fields are initialized, but these actions do not define instantiation. Rather, they are a subset of the actions that make up instantiation. The paragraph immediately preceding the one you quoted is quite explicit.Originally posted by Roger Chung-Wee:
So, as far as I am concerned, when the abstract class's constructor is executed, then that class is instantiated and (as the JLS says) its fields are therefore initialized.
If calling a class's constructor and initializing its fields meant that the class had been instantiated, then the quote above would be incorrect and the part you quoted should have readFrom JLS 8.1.1.1 in reference to an example:
The class Point cannot be instantiated because it is abstract.
I think that's about all I can say about it. I'm happy to agree to disagree, especially when it's about semantics. We're in agreement on what actions the JVM actually takes, and that's the important part.A subclass of an abstract class that is not itself abstract may be instantiated, resulting in the instantiation of the abstract class. (my emphasis)
Consider Paul's rocket mass heater. |