• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor in A class cannot be applied to given types  RSS feed

 
jamby vedar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code works



Below results in an error



So I am guessing that if you extend class, you should use super to pass objects?
 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a no-args constructor in ShapeDecorator? Because if not, your constructor won't compile except if you call a constructor of the superclass.
 
jamby vedar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark King wrote:Do you have a no-args constructor in ShapeDecorator? Because if not, your constructor won't compile except if you call a constructor of the superclass.


Here is the superclass below. So what's the problem here?

 
Mark King
Ranch Hand
Posts: 55
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this java tutorial starting from the section titled "Subclass Constructors" and the note almost at the bottom
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jamby vedar wrote:


So I am guessing that if you extend class, you should use super to pass objects?


Yes.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jamby vedar wrote:
Mark King wrote:Do you have a no-args constructor in ShapeDecorator? Because if not, your constructor won't compile except if you call a constructor of the superclass.


Here is the superclass below. So what's the problem here?


There's no problem in the super class; it's just that it doesn't have a no-arg constructor. It doesn't nee one, but here's the trick: if you don't explicitly call the superclass's constructor in the subclass, Java will "add" super(); for you. Notice that it has no args. So you have to call super with an arg or add a no-agrs constructor to the superclass.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!