Reputation: 9
basically I created a Person class and a constructor which sets the name,last name,age of the Person.all the properties of the class were set the private as it should be. I have made setters and getters for all the properties. On the main method I tried to override one of the setters just for practice reason. Its did draw an error saying Person.name not visible which means it cannot access private, Why this is happening, I mean if wasn't overriding the method it would have access. but if I set it to protected mode i will work. Here is the code:
class Person {
private int age;
private String name;
private String last_name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public Person(int age, String name, String last_name) {
this.age = age;
this.name = name;
this.last_name = last_name;
}
}
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person per = new Person(15,"bb","Sb") {
public void setName(String name) {
this.name = "aaaa";
}
};
per.setName("asdfaf");
System.out.println(per.getName());
}
}
Upvotes: 1
Views: 118
Reputation: 2878
This is called encapsulation . You can not access private vars from other classes . you can find more description here
Upvotes: 0
Reputation: 24157
You have created a class named Person
and in the following lines you are trying to create an anonymous subclass:
Person per = new Person(15,"bb","Sb") {
public void setName(String name) {
this.name = "aaaa";
}
};
As mentioned in doc:
A subclass does not inherit the private members of its parent class
Here your anonymous subclass is trying to access private field name
directly and so is the error. You can use getter/setter which are public. You can also check this related question on SO.
Upvotes: 1
Reputation: 4692
Basic idea behind overriding is to redefine existing functionality and give new definition to it. If you refer to documentation, private member variables are only accessible in it own class. That why it is not available in your anonymous sub-class implementation.
Note: Generally we do not override setter methods as they are not a functionality.
Upvotes: 0
Reputation: 393771
A private
member is only accessible in the class in which it is declared.
You created an anonymous sub-class of Person and tried to access a private
member of the super-class from the sub-class. This is never allowed.
When developers of a class wish to allow access to certain members of the class to its sub-classes, they set the acess level to protected
.
Upvotes: 1
Reputation: 2320
You cannot access private
fields from outside your class, even if you are overriding it. You are basically defining a new subclass of Person
in your main()
, which isn't allowed access to the private
field Person.name
. However, it can access a protected
field.
Upvotes: 0