top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

In Java, are constructors inherited? Can a subclass call the parent's class constructor? If yes, when?

+1 vote
443 views
In Java, are constructors inherited? Can a subclass call the parent's class constructor? If yes, when?
posted Apr 17, 2015 by anonymous

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

1 Answer

+1 vote

No, in java constructors can't be inherited to the subclass.

Yes, a constructor of a super class can be called from the subclass constructor by using the super calling statement and it should be the first statement in the subclass constructor.

Example:

class A
{
      A(int i)
      {
          System.out.println("cons-A");
      }
}

class B extends A
{
     B()
     {
           super(10);
           System.out.println("Cons-B");
     }
}
answer Jun 21, 2015 by Prakash Singh
...