Solved

child constructor and parent constructor, overriding and overloading

Posted on 2016-08-11
6
86 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 142

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 92
Configure a Bean in an XML file 4 33
JavaFX TableView not displaying correctly 3 19
Problem to App source 6 40
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

823 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