0

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?

Miru
  • 1
  • 6

1 Answers1

1

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();
 }
Neijwiert
  • 985
  • 6
  • 19
  • What i meant by that is that I;m trying to build a program with GUI where when i click the button, it will display all the information that i get from Building class – Miru Nov 23 '15 at 15:43
  • 1
    upvote for using strinbuilder, also small note:) http://stackoverflow.com/questions/247059/is-there-a-newline-constant-defined-in-java-like-environment-newline-in-c – HRgiger Nov 23 '15 at 17:37
  • @HRgiger +1 for `System` constant. – Neijwiert Nov 23 '15 at 17:41