• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructors  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi people,
I have a query on constructors related to a question in the SCJP Book by Kathy and Bates.

The question goes like this:

class Top{
public Top(String s){System.out.println("B"); }
}

public class Bottom2 extends Top{

public Bottom2(String s) { System.out.println("D"); }
public static void main(String[] args) {
new Bottom2("C');
System.out.println(" ");
}
}

The result of this is a compilation error and they have attributed this to "The implied super() call in Bottom2's constructor cannot be satisfied because there isn't a no-arg constructor in top."


I could not get what this statement meant.So can someone explain this concept.

Thanks in advance .
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

just FYI, whenever a constructor is invoked for a class, the first statement in a constructor is a call to the parent class default constuctor although nothing explicitly has been mentioned.

SO even under this case when your constructor for Bottom2 class is invoked, the first statement is substituted with super() which is a call to the super class no-arg constuctor.

So to overcome the below error you have two options
1) Define a no-arg constructor in parent Top Class
2) Make an explicit call to existing constructor of Top class

For example use your constructor of Bottom2 class as follows
 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Shashank for the inputs.Cheers .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!