Reputation: 13
For the following piece of code I am trying to implement encapsulation.
Have a doubt, if we have made the instance variables of the class dog
as private, how are we able to return the instance variable name
in the getName
method? As per my understanding, we cannot access a private instance variable outside the class in which it was created.
Can somebody explain how we are able to access the value of name
instance variable?
test method:
public class TestEncapsulationOld{
public static void main (String [] args) {
Dog d = new Dog();
d.setAge(5);
d.setName("tuffy");
System.out.println (d.getName()+"'s age is "+d.getAge());
}
}
class Dog
:
class Dog {
private int age;
private String name;
public void setName (String n) {
name = n;
}
public String getName() {
return name;
}
public void setAge (int a) {
if (a <= 0){
System.out.println ("Invalid age!!");
}
else if (a>20){
System.out.println ("A dog cant live that long");
}
else{
age = a;
}
}
public int getAge() {
return age;
}
}
Upvotes: 0
Views: 4997
Reputation: 599
While the variables themselves have private access within the class, the setters/getters are public and therefore accessible from anywhere. This means that you can call the method, which is then already inside the class, so the method has access to the variables themselves.
A very common use for these is when, for example, you want some instance variables that can only be read from the outside, ie. only have a getter. This is something you might want for example when you somehow generate the values, or take them in in the constructor and dont want them to be modifyable from the outside.
One example:
public class User{
private String name;
private String pass;
public User(String _name, String _pass){
name = _name;
pass = mySuperCoolHashingFunction(_pass);
}
private String mySuperCoolHashingFunction(String pass){
//do some stuff with the pass here
}
public void setPass(String newPass){
pass = mySuperCoolHashingFunction(newPass);
}
//other code, getters, setters, etc...
}
In this case noone can touch the pass variable directly, if you want to let them see the hashed password, you can have a getter, but otherwise when they use the setter, they arent actually setting the value directly, but it goes through some other changes.
Sorry for the long answer, just tried to be as clear as possible and provide some form of example, hope it helps :)
Upvotes: 1
Reputation: 8197
The getter method is public
,
public String getName() {
return name;
}
So you could access the the private variable as the method definition lies inside the same class of the variable
Upvotes: 0
Reputation: 1265
Yes, having the private
access modifier prevents you from accessing the attribute directly, but using a public
getter method, retrieves the attribute within the class, then returns to you the result.
Direct access example:
Dog d = new Dog();
String dogName = d.name;
Upvotes: 0
Reputation: 19284
name
field is private, means that you cannot use outside of Dog
class:
Dog d = new Dog();
System.out.println(d.name); // Won't compile as name is private.
However, getName()
method is public, so it can be used everywhere. Inside getName()
you can access name
field as it is in the same class. .
Upvotes: 1