Let's say I have the class User with the attributes name, email etc.. In my code, I still want to be able to update that information, but I don't want anyone being able to reach the setters methods directly.
public class User implements UserOperations {
private String name;
public User(String name) {
this.name = name
}
public String getName() {
return name
}
public void setName(String name) {
this.name;
}
}
public interface UserOperations {
String getName();
}
Is creating an interface with only the getters methods a good idea or should I change the access-capabilities of the User's method from public to private or protected?
I tried to implement the interface, but I feel as if it's not the best solution there is.
SOLID principles are the key answer for your concernes. In a way you want to separate logic of managing the state and managing operations you should create much more classes.
Here is possible solution: Your class User is pojo now. Making User class immutable will make impossible to change objects of this class.
To perform changes on object you would need to create a new object with new internal state. Usually it would be done with some other class as a facade which will perform required operations and return new object.
Effectively you would need some class UserOperationManager which will handle all required access to object
This approach will cover single responsibility and interface segregation principle