0

we know that new Instance() subclass in java , the first recursive call will be the parent class constructor,If the parent class is an abstract class,will be Calling its constructor? if this true ,Means that the parent class will be instantiated? But the abstract class can not be instantiated,Who can explain this question. Thank You.

  • *"But the abstract class can not be instantiated"* would be your answer. You can't instantiate an `abstract` class directly. If the class extends from an `abstract` class, then the child class's constructor is called and then the `abstract` parent's, depending on what super calls are made... – MadProgrammer Dec 27 '13 at 02:26
  • @MadProgrammer: The parent's constructor is always called, right? – Oliver Charlesworth Dec 27 '13 at 02:27
  • @OliCharlesworth: [yes](http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.5). `If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor`. – Jeroen Vannevel Dec 27 '13 at 02:29
  • @OliCharlesworth As far as I'm aware, the "default" constructor is called automatically, unless, you explicitly call a `super(...)` constructor – MadProgrammer Dec 27 '13 at 02:29

3 Answers3

4

You must understand that constructor is not responsible for creating instance, but only for initializing it. Creating instance is role of new keyword (this even returns reference for new created instance which constructor can't since it doesn't have return type).
So even when you are calling constructor of superclass (which can be abstract) you are not creating instance of that superclass, but you are executing code that will initialize fields inherited from it.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

Parent class constructor is always called irrespective of if its a concrete class or an abstract class. Instantiating the class is different from carrying it's definition to subclass. In case you instantiate the subclass , though you carried the structure and methods of the parent class in sub class object, it never means instantiation of parent class.

Vivek Vermani
  • 1,934
  • 18
  • 45
0

Abstract class constructor is actually used to be inherited by subclasses, because the constructor is equivalent to initialize method is called when the subclass constructor must call the parent class constructor, so you can generate when an object in a subclass abstract class abstract class to initialize the fields on demand and performs some initialization code. In fact, not necessarily to create an instance of a class before calling constructor, the subclass also need to call the parent class constructor. And it does not necessarily generate an instance constructor is called, in some implementations under special or exceptional circumstances, create an instance constructor is not called. The constructor calls does not necessarily generate an instance, but it must be an instance of the call, just like an ordinary instance method.

Javier
  • 12,100
  • 5
  • 46
  • 57