gudii9
asked on
child constructor and parent constructor, overriding and overloading
Hi,
I am running my code as below
when i run child class as java application i got below output
in Parent no arg construcot
in Child constructor
11
My question is since i am calling arument construcor of child as below
Child ch = new Child(4);
Am i not supposed to be called parent arg constructor (rather it is calling parent no argument constructor) . please advise.
i expected instead below output
in Parent no arg construcot
in Parent arg construcot
in Child constructor
11
I am running my code as below
public class Child extends Parent{
public Child(int i) {
// TODO Auto-generated constructor stub
System.out.println("in Child constructor");
}
public static void main(String[] args) {
Child ch = new Child(4);
ch.print();
}
}
public class Parent{
protected int number = 9;
public Parent() {
System.out.println("in Parent no arg construcot");
this.number = 11;
}
public Parent(int number) {
System.out.println("in Parent arg construcot");
this.number = number;
}
protected void print() {
System.out.print(number);
}
}
when i run child class as java application i got below output
in Parent no arg construcot
in Child constructor
11
My question is since i am calling arument construcor of child as below
Child ch = new Child(4);
Am i not supposed to be called parent arg constructor (rather it is calling parent no argument constructor) . please advise.
i expected instead below output
in Parent no arg construcot
in Parent arg construcot
in Child constructor
11
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
is there is a way i can see below kind of output
in Parent no arg construcot
in Parent arg construcot
in Child constructor
4
Basically i wan to see parent no arg as well arg constructors?
Constructor call must be the first statement in a constructor
please advise
in Parent no arg construcot
in Parent arg construcot
in Child constructor
4
Basically i wan to see parent no arg as well arg constructors?
public class Child extends Parent{
public Child(int i) {
// TODO Auto-generated constructor stub
super();
super(4);
System.out.println("in Child constructor");
}
public static void main(String[] args) {
Child ch = new Child(4);
ch.print();
}
}
if i write as aboave getting below error at line 5Constructor call must be the first statement in a constructor
please advise
No only one constructor is called at the parent class level. If you really need tha kind of functionality, I would create a new function that you call within the constructor. While you can only call one super call in your child class, immediately after you could call the function you declared in the parent class.
ASKER
Open in new window
Open in new window
above gave below output
in Parent arg construcot
in Child constructor
4
which makes sense