ICSE Class 10 Computer Applications Question 11 of 17

Encapsulation — Question 15

Back to all questions
15
Question

Question 9

What are the getter and setter methods?

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.