Python Programming - Encapsulation - Discussion
Discussion Forum : Encapsulation - General Questions (Q.No. 20)
20.
Consider the following Python code:
class Employee:
def __init__(self, name, salary):
self._name = name
self._salary = salary
def get_salary(self):
return self._salary
def set_salary(self, new_salary):
if new_salary > 0:
self._salary = new_salary
What concept of encapsulation is demonstrated in this code?Answer: Option
Explanation:
The single underscore prefix (
_
) in the variables _name
and _salary
indicates that they are protected variables, demonstrating encapsulation by hiding the implementation details.
Discussion:
Be the first person to comment on this question !
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers