top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the use of constructor in abstract class when an instance of that class can't be created in Java ?

0 votes
541 views
What is the use of constructor in abstract class when an instance of that class can't be created in Java ?
posted Jun 21, 2015 by Harshita

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

The use of constructor in abstract class is to basically complete the constructor chaining, if constructor chain does not complete it shows the compile time error.

As we know Object class is by default extended in java and when we extend a class by default compiler keep the default super calling statement for calling the default constructor of the super class and that reach upto the Object class constructor.

Example:

abstract class A
 {
   A()
   {}
 }
class B extends A
{
  B()
  {}
}

when the .class file is generated by the compiler. the compiler by default pass this to constructor and placed a super calling statement in the constructor.

Example: .class file reference for understanding purpose only

class A extends Object
{
    A(this)
    {
      super(this);
    }
}

class B extends A
{
    B(this)
    {
      super(this);
    }
}

when we instantiate class B

B b = new B();

class B constructor gives call to the constructor of class A which call to the Object class constructor.
In order to complete the constructor chaining, its required to reach the call upto the Object class constructor .

answer Jun 21, 2015 by Prakash Singh
...