Java Java Objects Meet Objects Add a Constructor

Krupa Shah
Krupa Shah
649 Points

GoKart.java

constructor body

GoKart.java
class GoKart {
  private String color = "red";
  public GoKart(String color)
  {
   private color=color;
  }
  public String getColor() {
    return color;
  }

}

1 Answer

Steven Parker
Steven Parker
203,226 Points

Since "color" has already been declared, you don't need the "private" keyword before the assignment.

And to differentiate between the field and the parameter with the same name, the field needs a "this." prefix:

    this.color=color;