top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

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

+1 vote
111 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
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...