-3

I have been trying to make three different extended classes from a superclass. The problem is that only the first extended class works fine while the other two gets an error when I name them, and its says they should be in their own file. I have looked around to see if someone had a similar problem, but nothing exactly like this. The extended class that is working is the first one called "Smycken".

here is the code:

abstract public class Vardesaker 
{
private String name;
double value;

public Vardesaker(String name, double value)
{
    this.name = name;
    this.value = value;
}

public String getName()
{
    return name;
}

 abstract public double getValue();
}

class Smycken extends Vardesaker
{
    private int adelstenar;
    private String material;
    public Smycken(String name, double value, int adelstenar, String material)
{
    super(name, 0);
    this.adelstenar = adelstenar;
    this.material = material;
}

public int getadelstenar()
{
    return adelstenar;
}

public String getMaterial()
{
    return material;
}

public double getValue()
{
    if(material.equalsIgnoreCase("guld"))
    {
        double sum = 2000 + (500*adelstenar);
        value = sum*1.25;
    }
    else
    {
        double sum = 700 + (500*adelstenar);
        value = sum*1.25;
    }
    return value;


    }
}


public class Aktier extends Vardesaker
{
    private double kurs;
    private int amount;

    public Aktier (String name, double value, int amount, double kurs)
    {
        super(name, 0);
        this.kurs = kurs;
        this.amount = amount;
    }

    public double getKurs()
    {
        return kurs;
    }

    public int getAmount()
    {
        return amount;
    }

    public double getValue()
    {
        double sum = (int) (amount*kurs);
        value = sum*1.25;

        return value;
    }


}

public class Apparater extends Vardesaker
{
    private double price;
    private int slitage;

    public Apparater(String name, double value, double price, int slitage)
    {
        super(name, 0);
        this.price = price;
        this.slitage = slitage;
    }

    public double getPrice()
    {
        return price;
    }

    public int getSlitage()
    {
        return slitage;
    }

    public double getValue()
    {
        double sum = price*(slitage/10);
        value = sum*1.25;

        return value;
    }
}
mackanmorre
  • 145
  • 1
  • 13

2 Answers2

3

It is not because of extends, it is because of public keyword for the other classes. If you create multiple class with public keyword then they should be in their own compilation unit.

Check this answer for why each public class should have separate file.

Vishrant
  • 15,456
  • 11
  • 71
  • 120
3

There is a simple rule - 1 public type (class,interface,enum) = 1 java file

Antoniossss
  • 31,590
  • 6
  • 57
  • 99