• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor Error when Extending a Class  RSS feed

 
David Evangelista
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program that should take a url and scan the page for any links. It is in the beginning stages, but I ran into an error when I tried to extend a class. There's a lot going on in this code, but the error is caused by the constructor.

Error message at compile time:
"constructor Page in class Page cannot be applied to given types;
{//Constructor
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length".

Here's the code(first my class, then the class I extended), thanks for the help!


 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first line of code in your SearchEngine constructor should call a constructor of its superclass, which is your Page class. It would do that like this:



However if you don't provide a call to one of the superclass's constructors, then the compiler will insert a call to a constructor with no parameters, which would look like this:



The basic rule is that any class's constructor (except the one in java.lang.Object) must call a constructor of the superclass, and that's how it's arranged in Java.

So since you didn't provide a call to super(), the compiler has inserted one for you. But there isn't a constructor of the form Page(), and that's what the error message means. You can fix that by providing a zero-argument constructor for Page, or by having your SearchEngine constructor start like this:



I don't know what string value you would pass there, but presumably your requirements might lead you to some suitable value. Alternatively, perhaps SearchEngine shouldn't be a subclass of Page; when I say "A SearchEngine is a Page" out loud, I don't find that convincing. But then I don't know much about your design, and perhaps a SearchEngine actually is a Page. According to your posted code, Page has only private members, so I'm assuming we aren't seeing the whole implementation.


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!