Throwing Error: variables has private access in class ~ Vehicle | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Throwing Error: variables has private access in class ~ Vehicle

public class Vehicle { private String color; private int ivalue; private double dvalue; Vehicle(String col) { this.setColor(col); } Vehicle(int ival,double dval) { this.setIvalue(ival); this.setDvalue(dval); } public void setColor(String col) { this.color=col; } public void setIvalue(int ival) { this.ivalue=ival; } public void setDvalue(double dval) { this.dvalue=dval; } public String getColor() { return color; } public int getIvalue() { return ivalue; } public double getDvalue() { return dvalue; } } public class Program { public static void main(String[] args) { Vehicle v1 = new Vehicle(5,12.1); System.out.println(v1.ivalue); System.out.println(v1.dvalue); Vehicle v2 = new Vehicle("orange"); System.out.println(v2.color); } }

21st Sep 2019, 5:12 PM
Madhavan Ananthan
Madhavan Ananthan - avatar
2 Answers
+ 2
Yes. the color property is private. You have to use the getColor() method.
21st Sep 2019, 5:15 PM
Airree
Airree - avatar
0
Thank You.....Doubt Cleared. ----------------------------------- public class Program { public static void main(String[] args) { Vehicle v1 = new Vehicle(5,12.1); System.out.println(v1.getIvalue()); System.out.println(v1.getDvalue()); Vehicle v2 = new Vehicle("orange"); System.out.println(v2.getColor()); } } Output: 5 12.1 orange
21st Sep 2019, 5:25 PM
Madhavan Ananthan
Madhavan Ananthan - avatar