Reputation: 955
According to the OOP concepts, encapsulation is considered as defined private variables and public getter and setter methods.
Example:
public class Student {
private String name;
private int id;
public void setName(String name){
name = this.name;
}
public void setID(int Id){
id= this.id;
}
public String getName(){
return name;
}
public int getID(){
return id;
}
}
But if I wrote this code in the following way, could I say this class follows encapsulation concept ?
Because here, we return department name by using public method.
public class Student {
private department;
public String getDepartmentOfStudent(String name){
// write java code to get department name based on name from DB
return department;
}
}
Case II: If private variable department
was not declared and just return value retrieved from DB, would we say that this class follows encapsulation?
Upvotes: 3
Views: 238
Reputation: 14998
To answer both questions we need take into account the concept of encapsulation
The localization of knowledge within a module. Because objects encapsulate data and implementation, the user of an object can view the object as a black box that provides services. Instance variables and methods can be added, deleted, or changed, but as long as the services provided by the object remain the same, code that uses the object can continue to use it without being rewritten. (...)
So, we can say YES, it does; because to the client consuming the information provided by Student
there is no knowledge of how the value of department
is being retrieved/handled and returned, it just knows that from the call to getDepartmentOfStudent
will be a String
result. It actually doesn't care whether it is comming from database or memory, only Student
knows that.
Upvotes: 1
Reputation: 1369
Yes your code follows encapsulation. In encapsulation the variables of the object cannot be directly accessed and modified by any other object.
In Case II:
If private variable "department" was not declared and there is no other public variables in your class, then yes you have encapsulation as the class's variables or data cannot directly accessed by another class.
Upvotes: 1
Reputation: 49
Getter
gets it's names due to it's functionality that it returns some private attributes not by it's name. For example: getName()
or getAge()
.
If a method named abc()
returns some private data member/attribute then it is a getter method.
Upvotes: 1
Reputation: 2427
For both cases, the answer is also "Yes". It is because your private field variable is not being accessed directly by other classes (e.g. declaring it as public String department
). Accessible department
is still being controlled by the code path of public String getDepartmentOfStudent(String name)
which is playing the role of a public getter.
Upvotes: 1
Reputation: 2670
But if I wrote this code in the following way, could I say this class follows encapsulation concept ?. Because here, we return department name by using public method.
=> Usually getter and setter are public. variables are private.
Case II : If private variable "department" was not declared and just return value retrieved from DB, would we say that this class follows encapsulation?.
=> Yes, its still encapsulation . It is not mandatory to have both getter and setter for every variable. So how you set data in variable, does not matter.
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates.Other way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield.
1 - Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared. 2 - As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding. 3 - Encapsulation can be achieved by: Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.
Source:- https://www.geeksforgeeks.org/encapsulation-in-java/
Upvotes: 1