+ 1

I can not understand how can there be''(v2.getColor)printing green output.

public class Vehicle { private String color; Vehicle() { this.setColor("Red"); } Vehicle(String c) { this.setColor(c); } // Setter public void setColor(String c) { this.color = c; } // Getter public String getColor() { return color; } } public class Program { public static void main(String[] args) { //color will be "Red" Vehicle v1 = new Vehicle(); //color will be "Green" Vehicle v2 = new Vehicle("Green"); System.out.println(v2.getColor()); } }

9th Aug 2018, 3:50 PM
Albert Whittaker :Shaken To Code
Albert Whittaker :Shaken To Code - avatar
1 Antwort
0
the 'c' parameter is set to green when calling the vehicle method. Remember, Vehicle(String c){...} overrides the Vehicle(){...} method. Hope this helps?
10th Aug 2018, 12:23 AM
Itoroabasi Unang
Itoroabasi Unang - avatar