I was wondering what was the design considerations in java that prevented classes like this?
public abstract class A{
public abstract A();
}
If we could force implementation of constructors,then we could instantiate abstract classes. But why didn't they? Does this violate OOP design or is it simply not possible?
Java constructor can not be abstract If we are declaring a constructor as abstract as we have to implement it in a child class, but we know a constructor is called implicitly when the new keyword is used so it can't lack a body and also it can not be called as a normal method.
Yes, we can define a parameterized constructor in an abstract class.
The main purpose of the constructor is to initialize the newly created object. In abstract class, we have an instance variable, abstract methods, and non-abstract methods. We need to initialize the non-abstract methods and instance variables, therefore abstract classes have a constructor.
An abstract constructor would have no meaning.
An abstract method forces all concrete sub-classes to implement a method of the same signature (which includes the same name).
However, a concrete sub-class can't implement a constructor having the same name as the "abstract constructor" of the abstract class, since a constructor must have the name of the class in which it appears.
An abstract modifier is meant for those whose implementation is yet to be given.
Considering a situation (like the one you just asked) the constructor itself is abstract so its class creation cannot actually happen.
Why
For a class to exist its default constructor will be invoked by the the system automatically. But now as you have provided your own constructor (which additionally is abstract), the default one won't exist and hence this class won't exist.
Hence an inconsistent situation to be in.
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With