Reputation: 29
I have3 classes: class Item :
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 ;
}
}
class Hardware:
public class Hardware extends Item {
private String proccesor;
private String motherboard;
private String RamMemory;
private String drive;
public Hardware(String name, int year,double price, String constructor, String proccesor,String motherboard, String RamMemory,
String drive) {
super(name, year, price, constructor);
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;
}
public String getProccesor() {
return proccesor;
}
public String getMotherboard() {
return motherboard;
}
public String getRamMemory() {
return RamMemory;
}
public String getDrive() {
return drive;
}
}
and class Proccesor:
public class Proccesor extends Hardware {
private double ghz;
private int cores;
public Proccesor (String name, int year,double price, String constructor, double ghz, int cores) {
super(super(name,year, price, constructor));
this.ghz= ghz;
this.cores= cores;
}
public void setGhz(double ghz){
this.ghz= ghz;
}
public void setCores(int cores) {
this.cores = cores;
}
public double getGHz() {
return ghz;
}
public int getCores() {
return cores;
}
}
I need to call the constructor of the Item class into the Proccesor class but i dont know how to do it. Command super(super(name,year, price, constructor)); gives me the error:
Proccesor.java:7: error: call to super must be first statement in constructor
super(super(name,year, price, constructor));
^
1 error
Upvotes: 0
Views: 74
Reputation: 9470
As stated above, super(super(...
is not a valid java syntax. Now, think about it:
Look at your Hardware
constructor.
Having only this constructor you explicitly allow to create Hardware
objects Only by supplying all the constructor params, including String processor
and motherboard
.
Now, because your Processor
is also a Hardware
it can only be constructed by supplying all necessary information to its super(...)
constructor.
What you could do, for example :
make additional another constructor in Hardware
which will allow you to construct objects with less params, and call it from Processor
's constructor.
or
call super(name, year, price, constructor, proccesor, motherboard, RamMemory, drive)
(with full params list) when constructing Processor
, perhaps passing some default values for params unused in Processor
, such as drive
.
Upvotes: 0
Reputation: 544
Do not use super(super(name,year, price, constructor)); Just use super(name,year, price, constructor) and it will call all another constructors with super successively
Upvotes: 0
Reputation: 38168
You can't call super(super(..))
.
You should only from class Processor call the constructor of your super class, then it will be in charge of calling the constructor of the Item
class.
If you need to bypass the constructor of Hardware, then Hardware should have a protected constructor that is designed to call the Item constructor and can be called by subclasses.
public Hardware(String name, int year,double price, String constructor, String proccesor,String motherboard, String RamMemory, String drive) {
this(name, year, price, constructor);
this.proccesor= proccesor;
this.motherboard= motherboard;
this.RamMemory= RamMemory;
this.drive= drive;
}
protected Hardware(String name, int year,double price, String constructor) {
super(name, year, price, constructor);
}
And, as you can see :
That's what the this(name, year, price, constructor);
statement means.
And indent your code better, it's hard to read.
Upvotes: 2
Reputation: 13596
Just call super()
, the superconstructor of Processor
(Hardware
) will call the constructor of Item.
Upvotes: 0