0
does the base class constructor automatically called whenever an object is created for subclass?
2 Antworten
+ 1
yes. whenever an object created in child class the constructor with no arguments in that class will be executed. as a part of child class constructor in the very first line compiler places super() means it's a call for parent class no argument constructor if parent doesn't have any no argument constructor then it calls default constructor of the parent class.
class Parent
{
Parent()
{
super();// compiler will place it automatically.
sop("parent class no argument constructor");
}
}
class Child extends Parent
{
Child()
{
super();// compiler will place it automatically.
sop(" child class no argument constructor");
}
P S V M(String...args)
{
new Child();
}
}
OUTPUT:
parent class no argument constructor
child class no argument constructor
conclusion: Though we are not creating object for parent class, because of that super(); which is placed by the compiler in every constructor control goes from child class no argument constructor to parent class no argument constructor and executing statements present in parent class no argument constructor, followed by child class no argument constructor statements execution.
0
tq Satath😀