• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructors, code won't compile  RSS feed

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm re-studying java again, and I don't understand why this code doesn't compile...


I don't see whats wrong with using that constructor in the subclass.

Any ideas?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the exact compiler error you are getting?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler will provide a default no-args constructor only if no other constructor is provided. Because you provided a constructor that takes ints, the compiler will not add a no-args constructor.

You can use super(args) as the first line of your constructor to call a specific constructor in the superclass (depending on the arguments). If you do not explicitly use super (or this), then there will be an implicit call to super() with no args.

So...
  • Because your subclass constructor does not explicitly call super(int x, int y), it is implicitly calling super() with no args. But...
  • Because you provided an (int x, int y) constructor for your superclass, the compiler did not create a default no-args constructor. Therefore, the call to FirstClass() will fail.
  •  
    colin shuker
    Ranch Hand
    Posts: 750
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, I think I know why...

    The constructor in the sub class calls the no-args constructor of super class automatically.

    But since I've put a different constrcutor in super class, then there is no free default no-args constructor, so it cant invoke it.

    So, I can either add a no-args constrructor to super class, or call super(x,y) in the subclass.

    Sound right?
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by colin shuker:
    ...So, I can either add a no-args constrructor to super class, or call super(x,y) in the subclass.

    Sound right?

    Exactly.

    (I didn't see Fred's response when I posted. But that's okay, because it looks like you didn't see mine when you followed. )
     
    Krishna Kadaveru
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Any time you extend a class, you must invoke the super constructor by a call to super(...) as the first line in youre extending classes constructor.
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't Wake The Zombies
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!