Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

default constructor must inside the Parent class. why?

 
Joe Man
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic