In Java, classes do not inherit constructors from their superclass. The reason your child class in that example has a no-parameter constructor is that Java auto-generates one for you if you don't define any other constructor. And the constructor it generates for you calls the no-parameter constructor of the superclass, which is why you get the output that you get in that example.
Also, in Java, you cannot override a constructor. Although a child class can have a constructor with same parameters as the superclass, it is not overriding it. Actually, the output that example will priduce is not what you claim it to be. If you don't explicitly call a superclass constructor from the constructor of a subclass, Java automatically calls the no-parameter constructor of the superclass for you if it exists. If it doesn't exist, you get a syntax error. In your example it exists so it will be called. In your example, you should see the result of the superclass constructor first and then the child class just like in your example where you explicitly called it. For Java, those 2 examples are functionally equivalent.
yes you are correct , that my mistake Thanks for pointing out
In Java, if a child class doesn't explicitly define its own constructor, it automatically inherits the default no-argument constructor from the parent class. However, if the parent class defines parameterized constructors, the child class doesn't automatically inherit them. You can manually call a parent constructor using the super() keyword within the child class's constructor.
Even in that case, it doesn't inherit the default no-parameter constructor. In Java, constructors are never inherited. There are 2 things going on in that case. If a class doesn't explicitly define a constructor, the Java compiler generates a default constructor for you that simply calls super(). The second thing is that if you do define a constructor but don't explicitly call a superclass constructor, the Java compiler inserts a call to super() in the bytecode that it generates.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In Java, classes do not inherit constructors from their superclass. The reason your child class in that example has a no-parameter constructor is that Java auto-generates one for you if you don't define any other constructor. And the constructor it generates for you calls the no-parameter constructor of the superclass, which is why you get the output that you get in that example.
Also, in Java, you cannot override a constructor. Although a child class can have a constructor with same parameters as the superclass, it is not overriding it. Actually, the output that example will priduce is not what you claim it to be. If you don't explicitly call a superclass constructor from the constructor of a subclass, Java automatically calls the no-parameter constructor of the superclass for you if it exists. If it doesn't exist, you get a syntax error. In your example it exists so it will be called. In your example, you should see the result of the superclass constructor first and then the child class just like in your example where you explicitly called it. For Java, those 2 examples are functionally equivalent.
yes you are correct , that my mistake Thanks for pointing out
In Java, if a child class doesn't explicitly define its own constructor, it automatically inherits the default no-argument constructor from the parent class. However, if the parent class defines parameterized constructors, the child class doesn't automatically inherit them. You can manually call a parent constructor using the super() keyword within the child class's constructor.
Even in that case, it doesn't inherit the default no-parameter constructor. In Java, constructors are never inherited. There are 2 things going on in that case. If a class doesn't explicitly define a constructor, the Java compiler generates a default constructor for you that simply calls
super(). The second thing is that if you do define a constructor but don't explicitly call a superclass constructor, the Java compiler inserts a call tosuper()in the bytecode that it generates.