Reputation: 29
Ok i have these classes here :
public class item {
public String name,constructor ;
public int year;
public double price ;
public item(String name,int year,double price,String constructor){
this.name = name ;
this.year = year ;
this.price = price ;
this.constructor = constructor ;
}
public String getName(){
return name ;
}
public int getYear(){
return year ;
}
public double getPrice(){
return price ;
}
public String getConstructor(){
return constructor ;
}
public void setName(String name){
this.name = name ;
}
public void setYear(int year ){
this.year = year ;
}
public void setPrice(double price){
this.price = price ;
}
public void setConstructor(String constructor){
this.constructor = constructor ;
}
}
and also class hardware:
public class hardware extends item {
private String proccesor;
private String motherboard;
private String RamMemory;
private String drive;
public hardware(String name, String proccesor,String motherboard, String RamMemory,
String drive ) {
super(name);
this.proccesor= proccesor;
this.motherboard= motherboard;
this.RamMemory= RamMemory;
this.drive= drive;
}
public void setProccesor (String proccesor) {
this.proccesor= proccesor;
}
public void setMotherboard(String motherboard){
this.motherboard= motherboard;
}
public void setRam(String RamMemory){
this.RamMemory= RamMemory;
}
public void setDrive(String drive){
this.drive= drive;
}
}
Now when i compile item, it compiles fine and no problems pop up.But when i try to compile the hardware class, cmd gives me this message:
Hardware.java.11: error: constructor item in class item cannot be applied to given types super(name);
Required :String,int,double,String
Found: String
Reason: actual and formal arguments lists differ in length
Upvotes: 1
Views: 77
Reputation: 43
When java attempts to compile hardware.java
it sees the statement super(name)
and looks for a constructor in class item
which takes a String
as its only argument, because that is how you are calling it.
However, the class item
only has one constructor defined, and it expects 4 arguments:
public item(String name,int year,double price,String constructor)
Because an item
must have a year, price, and contructor specified, you may need to add those fields to the constructor for the hardware
class, and then you could call super(name, year, price, constructor)
from your hardware constructor method.
Alternatively, you could modify the item
class to also provide another constructor, omitting the year, price, and constructor argument.
public item(String name){
this.name = name ;
}
After that change, super(name)
from the hardware constructor will now find the item(String)
constructor method in class item.
Upvotes: 2
Reputation: 7457
look at the item(String name,int year,double price,String constructor)
constructor.
you should call
super(name,year,price,constructor)
// change the parameters as you want but the types should remain
or add a constructor like this to the item class:
item(String name) { ... }
BTW class names should (not must) start with capital letters.
Upvotes: 1
Reputation: 4435
class item
doesn't have a constructor that just takes a String
parameter.
Upvotes: 0
Reputation: 10810
You're calling a constructor in the superclass that does not exist.
In your hardware constructor,
super(name);
Should be changed to call the only available constructor in item:
public item(String name,int year,double price,String constructor)
In Java, all arguments are required when calling a method. Unlike a language like Javascript, where if you don't pass something, an implicit undefined
is passed.
Upvotes: 2