1

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());
    }
}
8
  • 1
    Because that breaks the Encapsulation ! Commented Aug 21, 2015 at 5:00
  • 1
    Where are you overriding a method? I don't see this happening anywhere in the code you posted. Commented Aug 21, 2015 at 5:01
  • public void setName(String name) { this.name = "aaaa"; } Commented Aug 21, 2015 at 5:01
  • Hey, Tim method has been over ridden on instantation, Commented Aug 21, 2015 at 5:02
  • Ankur, It possible in c++, how is that breaks Encapsulation? Commented Aug 21, 2015 at 5:03

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you sir, i didn't realise i was creating another class. ,now everything make sense. Thanks!, but how does it inherit private properties?
@jojo Specifically, this is called an anonymous subclass.
So it inherit private but does not allow access, how weird is that? cause look the constructor actually change the name, and when i do a getter it does return the name
@jojo You can still access and change the name member via the public getter and setter methods, so you do have access. You just can't access the private member directly.
So its not like a child class. , its a special type of class?
|
1

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.

2 Comments

Akhil, how can you say it does not inherit if the constructor itself accesses private properties. and then when i make a getter on the new object which was from sub class it gets it?
@jojo constructor access the private members because it's allowed to do so, like public setter method
0

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.

Comments

0

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.

5 Comments

Naman, but i do have access since i can just do , per.getName(), and it will get the name the was set on the constructor even if its a sub class
Kindly refer documentation of access modifier. In that it is clearly mentioned that public members(getName()) are accessible to world but private members(this.name) are only accessible to its own class methods and constructors.
Naman, I got the idea, When i call super constructor i will inherit private properties which accessed by parent class since it will make those properties to the new object. nvm I got the principle here.
Refer Inheritance in java. And first get clear idea of super class-sub class relation and after that try to understand anonymous sub-class.
Just to clarify, if by inherit, you mean to use it in child class then you are misunderstood. It will stay in super class only and is accessible via constructor, getter and setter in sub class.
0

This is called encapsulation . You can not access private vars from other classes . you can find more description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.