public class Normal {
public string name; // name is public
public String getName() {
return name ;
}
public String setName(String newName) {
The idea of getters and setters are to allow developers to execute other required logic to derive the value for a class field. Therefore leaving the class fields as public allows to skip executing the logic before assigning a value to that field.
The second class is easier to maintain as one can be sure that the value of the name variable is always set through the setter.
Again the second class is better for re-factoring as it exposes less members.
If you imagine some other developer using the "normal" class, he may choose to set the value of name directly, instead of using the setter. Therefore if in future you decide to change the visibility of the "name" variable, you'll break the other persons code. In such a simple example this isn't important, but when writing important code, this tends to become problematic since you don't want to break other developers code by change the visibility of a variable. Hence not encapsulated code is unchangeable.
Generally speaking, a better design tends to expose less moving parts. But in addition to that encapsulation narrows the number of ways that the internals of your class can be manipulated by other codes.