Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

child constructor and parent constructor, overriding and overloading

Hi,

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();
    }
}

Open in new window


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);
    }
}

Open in new window


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
Avatar of Jeffrey Dake
Jeffrey Dake
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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);
    }
}

Open in new window


public class Child extends Parent{
    public Child(int i) {
		// TODO Auto-generated constructor stub
    	super(4);
    	System.out.println("in Child constructor");
	}

	public static void main(String[] args) {
		Child ch = new Child(4);
        ch.print();
    }
}

Open in new window


above gave below output

in Parent arg construcot
in Child constructor
4


which makes sense
Avatar of gudii9

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?
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();
    }
}

Open in new window

if i write as aboave getting below error at line 5
Constructor 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.