• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Java SonofBoo question

 
Peter Pay
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In ch.9 of this book, it talks about constructors.

It asks you to id the subclass constructors that would not compile. I id 2 of the 3. The one I am not sure of is:

//super class

public class Boo {
public Boo(int i) {}
public Boo(String s) {}
public Boo(String s, int i) {}

//subclass

class SonOfBoo extends Boo {

public SonOfBoo(int i, String s) {} //This is the one I am not sure.
//If it has a statement: super(i, s);
// I know it would be wrong because the variables' type does not match up with the order of variable type in the superclass's constructors

However, since it has no super statement at all, does it just mean it is an invalid constructor?? I am not sure if the author is suggesting there is no statement under this sublass constructor or it is suggesting there are statements implied.

If someone can help me, I would be appreciated.

Thanks!





 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Pay wrote:However, since it has no super statement at all, does it just mean it is an invalid constructor?? I am not sure if the author is suggesting there is no statement under this sublass constructor or it is suggesting there are statements implied.

If someone can help me, I would be appreciated.

If you don't supply a super(whatever) statement yourself, the compiler will add
super();

So, the question you need to ask yourself is: does the superclass have a constructor that matches what the compiler adds?

Winston
 
Avinash Ga
Ranch Hand
Posts: 78
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you try to compile this


there will be a compilation error saying that "class Boo have no constructors which takes zero arguments". Because the compiler will implicitly create a Default (Parameterless) constructor if and only if there are no other constructors explicitly defined.

Best Wishes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic