0

Here are my class's feature: enter image description here

I have to override toString method to make each class print out:

“[Superclass]: [name-of-superclass] ---[subclass]: [name-of-subclass]”

and I have getter getDescription for each classes' name

I know for subclasses, they only have to do super.getDescription and this.getDescription.

  1. But I don't understand for the superclass LivingRoomItem, how can it call all the getDescription from its subclass?

EDIT:

class livingRoomItem{
    int id;
    double price;
    String color;
    String description = "livingRoomItem";

    livingRoomItem(int id, double price, String color, String description){
        this.id = id;
        this.price = price;
        this.color = color;
        this.description = description;
    }

    @Override
    public String toString(){
        return "[Superclass] :" + this.description + "----" + "[subclass]: ";
    }


 public String getDescription() {
    return "livingRoomItem";
}

public void setDescription(String description) {
    this.description = description;
}


class Sofa extends livingRoomItem{
private int seater;

Sofa(int id, double price, String color, String description, int seater){
    super(id, price, color, description);
    this.seater = seater;
}
@Override
public String getDescription(){
    return "Sofa";
}

public int getSeater() {
    return seater;
}

public void setSeater(int seater) {
    this.seater = seater;
}
 @Override
public String toString(){
    return "[Superclass] :" + super.getDescription() + "----" + "[subclass]: "+ this.getDescription() ;
}

}

Shin Yu Wu
  • 1,129
  • 4
  • 14
  • 23

1 Answers1

1

Your line of thinking is right, maybe only your execution is not.

Considering the following classes:

public class LivingRoomItem
public class Sofa extends LivingRoomItem
public class Table extends LivingRoomItem
public class Lighting extends LivingRoomItem

You can handle all of them as they are LivingRoomItem

In your calculatePrice method, you will use the LivingRoomItem as expected to handle all the objects:

public static BigDecimal calculateTotalPrice(List<LivingRoomItem> items) {
    return items.stream().map(item -> item.getPrice()).reduce(BigDecimal.ZERO, BigDecimal::add);
}

Since you are instantiating all the objects as themselves (not as LivingRoomItem), and setting the price, it should work properly:

public static void main(String[] args) {

    Sofa sofa = new Sofa();
    sofa.setPrice(BigDecimal.valueOf(200L));

    Lighting lighting = new Lighting();
    lighting.setPrice(BigDecimal.valueOf(652L));

    Table table = new Table();
    table.setPrice(BigDecimal.valueOf(598L));

    System.out.println(PriceUtil.calculateTotalPrice(Arrays.asList(sofa, lighting, table)));
}

Result: enter image description here


I got your point in your toString, for the purpose of your exercise you can use the instanceof operator inside your superclass toString:

Example (inside your LivingRoomItem class):

return (this instanceof Sofa ? "Superclass... Subclass: Sofa" : "Regular LivingRoomItem toString");

But like I said, I believe it is right to show Object as superclass :) And only show LivingRoomItem on the subclasses.

Hope I was able to help you!

nortontgueno
  • 2,553
  • 1
  • 24
  • 36
  • Thank you!! Can you tell me how to override the toString method in livingRoomItem to make it return “[Superclass]: [name-of-superclass] ---[subclass]: [name-of-subclass]” ?? (My first question on the top) – Shin Yu Wu Nov 30 '18 at 01:04
  • Try to override the toString method on each subclass, and call the getSuperClass() and getClass(). You can.find an example over here: https://stackoverflow.com/questions/9790210/why-super-getclass-in-a-subclass-returns-subclass-name&ved=2ahUKEwjkmoSM9_reAhWDTJAKHR9DBcIQFjAAegQIBxAB&usg=AOvVaw1Jg3UKAWQQfeYSsHHdfSLa – nortontgueno Nov 30 '18 at 01:11
  • Yes, I've already override all the subclass, and they are work. But I am stuck in the superclass livingRoomItem toString method. How can I call all it's subclass's getDescription method? Because when I use getClass().getSuperclass() on my superclass, the output is class java.lang.Object, rather than Sofa, Table, Lighting – Shin Yu Wu Nov 30 '18 at 01:19
  • Would you mind edit the question and add your current toString method for all those classes? And just for curiosity, why would you ever need that your superclass see your subclasses, since you are already working with the subclasses? – nortontgueno Nov 30 '18 at 01:33
  • I've edited it. Since most subclassed are similar, I just post my superclass livingRoomItem and subclass Sofa. Well... because this is my practice question in java class... We have to familiar with how to use inheritance and override method. – Shin Yu Wu Nov 30 '18 at 01:42
  • @ShinYuWu, just complemented the answer. Thanks! – nortontgueno Nov 30 '18 at 10:48