Computer Applications
What are the getter and setter methods?
Encapsulation & Inheritance in Java
2 Likes
Answer
Getter methods are used to read values of private data members of a class which are directly not accessible in non-member methods. They do not modify the data members. They should have "public" access modifier and return type same as the data type of that instance variable. A getter method simply returns the instance variable's value.
Setter methods allow us to change the values of an instance variable of a class. They should have "public" access modifier and "void" return type.
Answered By
1 Like