Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

child constructor and parent constructor, overriding and overloading

Posted on 2016-08-11
6
Medium Priority
?
123 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
[X]
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
6 Comments
 
LVL 11

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 500 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 500 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 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 11

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to implement Singleton Design Pattern in Java.
Progress
Suggested Courses

636 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