Reputation: 1
I am trying to use the inheritance concept where I inherit my data from the super class using GUI in Netbeans. I want the button in the form to display the information i got from the super class. However,i am getting the 'Void cannot be allowed' error. Can you help me solve this?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
House myHouse = new House(8);
JOptionPane.showMessageDialog(null,myHouse.displayHouseDetails());
}
UPDATE
public class Building {
protected String size;
protected double price;
public Building(String size,double price)
{ this.size = size;
this.price = price;
}
}
public class House extends Building{
private int houseNo;
public House(int houseNo)
{ super("100 X 90",100000);
this.houseNo = houseNo;
}
public void displayHouseDetails(){
System.out.println("House no:"+houseNo);
System.out.println("Size:"+size);
System.out.println("Price:"+price);
}
btw, may I know how can i make my button to display the data that inherit from super class?
Upvotes: 0
Views: 252
Reputation: 1025
public void displayHouseDetails()
returns void
and may not be used as an argument in another function.
It should be:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
House myHouse = new House(8);
myHouse.displayHouseDetails();
}
Or make a concatenated string in the function and return that.
btw, may I know how can i make my button to display the data that inherit from super class?
Change displayHouseDetails
to:
public String displayHouseDetails(){
StringBuilder builder = new StringBuilder("House no:");
builder.append(houseNo).append(System.lineSeparator());
builder.append("Size:").append(size).append(System.lineSeparator());
builder.append("Price:").append(price).append(System.lineSeparator());
return builder.toString();
}
Upvotes: 1