Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

child constructor and parent constructor, overriding and overloading

Posted on 2016-08-11
6
88 Views
Last Modified: 2016-08-13
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
0
Comment
Question by:gudii9
6 Comments
 
LVL 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 125 total points
ID: 41753366
you need to call super(i); as your first call in your child class constructor. This will tell the parent class to use the constructor with the argument.
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 125 total points
ID: 41753396
public static void main(String[] args) {
            Child ch = new Child(4);
        ch.print();
    }

note that this will create a new child object, and not set the "current" object itself with an argument...

so, when you run the code, and Child being the "main class", it will first create that main class (with the parent no arg constructor), and then run main(), which creates a new Child object with argument...
0
 
LVL 14

Accepted Solution

by:
CPColin earned 250 total points
ID: 41753947
Jeff is right. Per this section of the language specification:

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

So the constructor in your Child class, as it's currently written, is calling the no-arg Parent() constructor.

Guy,

so, when you run the code, and Child being the "main class", it will first create that main class (with the parent no arg constructor), and then run main(), which creates a new Child object with argument

Since main is static, the JVM does not need to create an instance of Child before executing it.
1
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 7

Author Comment

by:gudii9
ID: 41754560
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 41754563
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
0
 
LVL 10

Expert Comment

by:Jeffrey Dake
ID: 41754566
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question