-1

Lets say we have two classes:

public class A{
  public A(){
    System.out.println("A");
  }
}

and its subclasas

public class B extends A{
   public B(){
      System.out.println("B");
   }
}

The output below would be A A B

public static main(String[] args){
 A a = new A();
 B b = new B();
 }

WHY like this is constructor is not inherited? SHould not we call super() in subclass constructor to call constructor of parent?

thanks

7
  • no. that is automatically arranged by the vm. when you look at compiled code, the call will be there. Commented Oct 5, 2017 at 9:15
  • Why do you think "constructor inheritance" is related to "calling super constructor when building a subclass"? Commented Oct 5, 2017 at 9:15
  • You are not inheriting a constructor, when you create an instance of B it will first call the appropriate constructor to create A. Commented Oct 5, 2017 at 9:17
  • It's unclear what your question is...but the results you show are as I would expect from the code posted. Commented Oct 5, 2017 at 9:18
  • @Stultuske Please be precise about wording. The VM has nothing to do with this - it is the compiler inserting the corresponding byt3e code instructions. Commented Oct 5, 2017 at 9:34

1 Answer 1

0

when you create object of child class then super() call automatic by jvm for non-parameterized constructor.

and if your constructor is parameterized then you have to call by passing parameters like super(parameters).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.