Solved

child constructor and parent constructor, overriding and overloading

Posted on 2016-08-11
6
71 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now