Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor Overriding in Java

Can we override a constructor of super class in sub class of the same class. If yes, how? If no, why?

class Super{}
class Sub extends Super
{
    //write code, if yes
}
like image 639
Jagruttam Avatar asked Jun 24 '26 20:06

Jagruttam


1 Answers

No, you cannot override the constructor of the Super class. JVM will definitely call the super class constructor while creating the child class instance. So, whenever you create a subclass instance, it will invoke the baseclass constructor and then continue with the subclass constructor statements. Constructors are not Methods that can be overriden

like image 112
Keerthivasan Avatar answered Jun 27 '26 11:06

Keerthivasan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!