default constructor and arguments

gudii9
gudii9 used Ask the Experts™
on
class Parent {
	//Parent(){}
	Parent(){}
    Parent(int number) {
    }
}
 
class Child extends Parent {
    Child(int a, int b) {
    }
}

Open in new window


for above code do not i need to give  constructor as below with two arguments?


      Parent(int a, int b){}
//if i give constructor like above does it still called default constructor?

what is the meaning of below sentence?
4. The default constructor is only generated by the compiler:
If we explicitly write a constructor that looks exactly the same as the default constructor, it is NOT called the default constructor.
https://www.codejava.net/java-core/the-java-language/9-rules-about-constructors-in-java
Please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
For the parent class you have defined a constructor with no and with one parameter. This are the valid ways to create a parent object then.
To create a child object you need to provide two parameters, because there is no other constructor defined.

The mentioned default constructor is a definition made by the compiler only if you have not provided any constructor. The default constructor never has parameters.
The constructor parent() of your code looks like a default constructor, and is what the compiled would create. Nevertheless, it is explicit and hence not called a default constructor.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial