Reputation: 73
I was writing a Volume and Book class in Java, to help me better understand Constructors and Objects - basically the broader aspects of OOP. When I try to create a main class, I receive an error that states the following:
"constructor Volume in class Volume cannot be applied to given types; required: String, int, Book[] found: no arguments reason: actual and formal lists differ in length ----"
Here is the code I have so far.
First, the Volume class:
public class Volume extends Book{
public String volumeName;
public int numberOfBooks;
public Book[] books;
// Constructor with parameters
public Volume(String volumeName,int numberOfBooks,Book[] books){
this.volumeName = volumeName;
this.numberOfBooks = numberOfBooks;
this.books = new Book[numberOfBooks];
}
// String representation of the Volume
public static String toString(Volume volume){
String volumeDescription = "Here are the details of the selected Volume:\n";
volumeDescription += "The volume's name is \"" + volume.volumeName + "\".\n";
volumeDescription += "It contains " + volume.numberOfBooks + " books.\n";
volumeDescription += "Here is a list of books it contains:\n";
for(int i = 0; i < volume.numberOfBooks; i++){
volumeDescription += "[" + i + "] " + volume.books[i];
}
return volumeDescription;
}
// Description of each book in the Volume
public static String getBookArray(Volume volume){
Book[] listOfBooks = volume.books;
String bookDescriptions = "Here is a description of each book in the Volume.\n";
for(int i = 0; i < listOfBooks.length; i++){
bookDescriptions += "Book #" + i + ":\n";
bookDescriptions += "Title: " + listOfBooks[i].title + "\n";
bookDescriptions += "Author: " + listOfBooks[i].author + "\n";
bookDescriptions += "Number of pages: " + listOfBooks[i].numberOfPages + "\n";
}
return bookDescriptions;
}
}
And here is main, where I receive the above error I mentioned:
public class Volume_Main extends Volume{
public static void main(String[] args) {
// TODO code application logic here
}
}
I have the Constructor in Volume properly set up (to my understanding), but still receive this error. Any tips or suggestions? Thank you in advance!
Upvotes: 2
Views: 1004
Reputation: 3456
Your Volume class constructor is a no arg constructor. And you are extending Volume class in Volume_Main class. By default it will look for no arg constructor in Volume class.
So in Volume_Main class you have to define constructor that will call proper constructor of Volume class.
Edit:
Two ways you can resolve this issue.
1) if you don't have any need to extend Volume class then you can remove 'extends Volume'
from Volume_Main class.
2) You can define a no arg constructor in Volume class that will call your defined parameterised constructor like
public Volume() {
this(voulmeName, numberOfBooks, books);
}
Upvotes: 0
Reputation: 3592
Or you remove extends Volume
or you create a contructor with the same parameters in Volume_Main. Personally I think inhering from Volume in this case has no sense.
Upvotes: 0
Reputation: 38345
In Java, every class has to have a constructor. In the case that you don't explicitly define one the compiler essentially inserts an empty constructor for you. Constructors also implicitly call the parameterless constructor of the parent class, if you haven't explicitly called a superclass constructor yourself. So your Volume_Main
class has the following constructor:
public Volume_Main() {
super()
}
However, the parent class of Volume_Main
is Volume
, and that has no parameterless constructor, so the compiler throws an error. There's probably no reason for Volume_Main
to extend Volume
, but at the very least you're going to have to explicitly define a parameterless class in Volume
.
Upvotes: 2