I am using Java
public class Drink {
private String contents;
Drink(String theContents){
contents = theContents;
}
public static void main(String[] args) {
Drink water = new Drink("water");
Drink oj = new Drink("orange juice");
Drink cocaCola = new Drink("Coca Cola");
oj.contents = "not orange juice";
System.out.println(oj.contents);
}
}
I thought the output should be an error. e.g.the line oj.contents = "not orange juice";
should produce an error.
If this is wrong please tell me why :) thanks
Public variables, are variables that are visible to all classes. Private variables, are variables that are visible only to the class to which they belong. Protected variables, are variables that are visible only to the class to which they belong, and any subclasses.
variable content is not visible to other classes because it is private. But you can access it anywhere in the class Drink
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments