permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes default constructor must inside the Parent class. why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "default constructor must inside the Parent class. why?" Watch "default constructor must inside the Parent class. why?" New topic
Author

default constructor must inside the Parent class. why?

Joe Man
Ranch Hand

Joined: Feb 17, 2002
Posts: 71
Why do we need a default constructor inside the Parent class when we create an instance of the Parent class?
public class Parent{
public Parent(String test){
System.out.println(test);
}

}
class Child extends Parent{
public Child(String x){
System.out.println(x);

}
public static void main(String [] args){
Child theChild = new Child("calling Child class");

}
}
If i don't add the default constructor "public Parent(){}" inside the parent class, the compiler complains:
Child.java:2: cannot resolve symbol
symbol : constructor Parent ()
location: class Parent
public Child(String x){
^
1 error

Why is that?


Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, Joe
When you construct a class, all its parents will be constructed at first. For example.
class A{}
class B extends A{}
class C extends C{}
When you constrict class C(C c1=new C()), the following happens
C c1 = A().B().C(). (it calls empty(default) constructors for each parent class...)
And, for example if class B has not default
public B(){} constructor, then you'll get error. In this situation you had to call ecplicitly this parent constructor. Example:
class A{}
class B extends A{
public B(String) {}
}
class C extends C{
public C() {
super("Hi");
}
}

And in your example you had to call super(""); in your Child() constructor. That's all.
Jam
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Joe, when you create a class and don't define any ctor like,
class foo {}
java defines and inserts a default ctor for you in your code during the compile phase, so your class with the default ctor look like this when java get's throught with it,

note that a call to super() is added as a call within the added default ctor.
When you define your own ctor, java will not define a default ctor for you, take a look at the following code,

but once again java will insert a call to super() within your custom defined ctor, unless you make a call to another ctor using the this or super operator. The previous class gets a call to super added inside the "foo(int i)" ctor during the compile phase.

As you know, all classes require a ctor so that a object from in can be created, also all classes you define implicitly subclass class Object!
If another class were now to extend class foo, the compiler would complain about a default ctor not being define in the super class. The following code will not compile (as you've noted),


class bar fails to compile because java will create a default ctor with a call to super() inside it, the super class foo has no default ctor defined so you get a compile error.
class bar2 will compile since it correctly makes a call for the superclass's defined ctor.


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: default constructor must inside the Parent class. why?