-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

Ann
  • 193
  • 1
  • 1
  • 12
  • no. that is automatically arranged by the vm. when you look at compiled code, the call will be there. – Stultuske Oct 05 '17 at 09:15
  • Why do you think "constructor inheritance" is related to "calling super constructor when building a subclass"? – Tom Oct 05 '17 at 09:15
  • You are not inheriting a constructor, when you create an instance of B it will first call the appropriate constructor to create A. – IllegalArgumentException Oct 05 '17 at 09:17
  • It's unclear what your question is...but the results you show are as I would expect from the code posted. – SPlatten Oct 05 '17 at 09: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. – GhostCat Oct 05 '17 at 09:34
  • The main thing to look at: https://stackoverflow.com/a/1644363/1531124 – GhostCat Oct 05 '17 at 09:36
  • @GhostCat the compiler puts it there, the vm detects it on runtime. where-as my comment was far from complete, and could/should be worded better, on runtime, it is not handled by the compiler. – Stultuske Oct 05 '17 at 09:46

1 Answers1

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).

Mostch Romi
  • 531
  • 1
  • 6
  • 19