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());
    }
}
Buddy
  • 10,874
  • 5
  • 41
  • 58
jojo
  • 9
  • 4
  • 1
    Because that breaks the Encapsulation ! – Ankur Anand Aug 21 '15 at 05:00
  • 1
    Where are you overriding a method? I don't see this happening anywhere in the code you posted. – Tim Biegeleisen Aug 21 '15 at 05:01
  • `public void setName(String name) { this.name = "aaaa"; }` – saagarjha Aug 21 '15 at 05:01
  • Hey, Tim method has been over ridden on instantation, – jojo Aug 21 '15 at 05:02
  • Ankur, It possible in c++, how is that breaks Encapsulation? – jojo Aug 21 '15 at 05:03
  • The whole purpose of `protected` fields and methods is to say that they are accessible by subclasses. Java gives you both so that you can decide whether you want subclasses to be able to see those fields and methods. You said `private`, which means you didn't want the sublcass to see them. – ajb Aug 21 '15 at 05:06
  • in your code , you have problem with this line this.name = "aaaa"; use setName instead – Rohan Pawar Aug 21 '15 at 05:07
  • if you really want to do this , in the overridden method you may use super.setName("aaaa"). but OOP is basically about behaviors of classes. we extend a class, we should extend the behavior and we should not alter the behavior of the super class. so this cleary violates the LSP – hunter Aug 21 '15 at 07:14

5 Answers5

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 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 Aug 21 '15 at 05:06
  • @jojo Specifically, this is called an _anonymous subclass_. – ajb Aug 21 '15 at 05:07
  • 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 Aug 21 '15 at 05:10
  • @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. – Eran Aug 21 '15 at 05:12
  • So its not like a child class. , its a special type of class? – jojo Aug 21 '15 at 05:14
  • @jojo If by child class you mean a sub-class, then it's exactly like a child class. – Eran Aug 21 '15 at 05:25
  • Eran, but look at this, when i instantiate the object which has an overridden class, i can do object.getName(), which means if constructor runs it will inherit private props, you see what i mean? – jojo Aug 21 '15 at 05:31
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.

Community
  • 1
  • 1
akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
  • 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 Aug 21 '15 at 05:28
  • @jojo constructor access the private members because it's allowed to do so, like public setter method – Ankur Anand Aug 21 '15 at 06:24
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.

saagarjha
  • 2,221
  • 1
  • 20
  • 37
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.

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • 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 – jojo Aug 21 '15 at 05:19
  • Kindly refer [documentation](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) 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 Gala Aug 21 '15 at 05:22
  • 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. – jojo Aug 21 '15 at 05:25
  • Refer [Inheritance](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) in java. And first get clear idea of super class-sub class relation and after that try to understand anonymous sub-class. – Naman Gala Aug 21 '15 at 05:33
  • 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. – Naman Gala Aug 21 '15 at 05:35
0

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

Abdullah Al Noman
  • 2,817
  • 4
  • 20
  • 35