-1

As per the link, definition says, The private modifier specifies that the member can only be accessed in its own class.

But the below code is able to access private member item of super class in sub class.

class SuperType {
    private int item;
    public void setItem(int item){
        this.item = item;
    }
    public int getItem(){
        return item;
    }
}

public class SubType extends SuperType{
    public static void main(String[] args){
        SubType s = new SubType(); 
        s.setItem(2);
        System.out.println(s.getItem());

    }
}

It is also understood that s.itemdoes not work, because item is not a member of SubType class.

How do i understand this definition?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • so, the meaning of this definition: `The private modifier specifies that the member can only be accessed in its own class.` is that you cannot say, `s.item`. This definition is using the word `access` in terms of accessing the member directly `s.item`. He does not talk about indirect access, am i correct? I guess this was my question. – overexchange Nov 19 '14 at 00:28

3 Answers3

4

Access modifiers affect direct access to that member.

SubType can get access to item indirectly, through the public getter method, which exists in SuperType and thus has access to the private item member variable.

But SubType can't directly access it, i.e. this is an error if in SubType:

s.item  // error; private in SuperType
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • OK. I got you. Another question, When we design classes, we assign access modifier to any member(data/behaviour) of a class thinking in terms of `direct access` but not `indirect access`. am i correct? – overexchange Nov 19 '14 at 00:33
  • Yes, the access modifier controls direct access. Note that this plays an important part in data encapsulation, where member variables are usually `private` and methods are `public`, so the methods provide indirect and controlled access to the data. – rgettman Nov 19 '14 at 00:36
  • am yet to learn the meaning of `data encapsulation`, but my further question is, I understand, reference variable `s` of type `class SubType` has direct access to member(behaviour) `setItem()`, so we are able to say `s.setitem(2);` but `setItem()` being a member of `class SubType` after inheritance, How can this member `setItem()` access member `private item` of `class SuperType`? I think member `setItem()` of `class SuperType` should only be able to access `private item`. because definition says: `... in its own class` & `s.setItem()` is member of `class SubType` but not of `class SuperType`. – overexchange Nov 19 '14 at 00:45
  • The `setItem` method is inherited by `SubType`, but it's defined in `SuperType`, so the code for `setItem` has access to `SuperType`'s `private` member variables. – rgettman Nov 19 '14 at 00:49
  • you mean, there is a difference in overriding & inheriting a method in subclass. inherited method can access private members of super class, overridden method cannot access private member of super class. – overexchange Nov 19 '14 at 00:54
  • True. If you were to override `setItem()` in `SubType`, that code would not have access to the `item` member variable. However, it could still call `super.setItem(item)`, to set the variable's value indirectly. – rgettman Nov 19 '14 at 00:56
  • i had one last question, Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65181/discussion-between-overexchange-and-rgettman). – overexchange Nov 19 '14 at 00:57
1

You have the definition correct and you are accessing a private field by a public accessor and mutator. What you can't do is,

s.item = 2;

which you could if item was public. Also, the fact that SubType is a sub-class of SuperType is irrelevant here. Every class can access item by it's public accessor (and mutator).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0
  1. You use getter and setters methods to acces/mutate private members in a class. The getters and setters are declared as "public".

  2. You can extend a class and inherit the parent's private members and the getters and setters to access them.

  3. The solution or workaround is to use "Protected" access specifier that allows you to directly access/mutate the properties of a class without any getters and setter. The usage scope is only the package where the class and other classes will be. Protected doesn't require class inheritence. As long as the classes are within the same package, they can access the neighbour's protected members.

A very good explanation has been give in SO in the past - In Java, difference between default, public, protected, and private

Community
  • 1
  • 1
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108