-1

Possible Duplicate:
Are private fields inherited by the subclass?

It's no doubt that the subclass can't access the private fields directly. But if the private fields exists? Actually i can access the private fields by its public or protected methods.

the private methods either.

Is there anyone can tell me what's this going on ?

Community
  • 1
  • 1
YiFan Wu
  • 123
  • 5
  • 1
    This has to be a duplicate question. – Wug Oct 09 '12 at 13:44
  • There are a few that are close: There's http://stackoverflow.com/questions/5065053/java-class-inheritance-private-fields-accessible-in-subclass-why, and also http://stackoverflow.com/questions/4501678/subclass-needs-access-to-private-attribute-of-abstract-superclass – Wug Oct 09 '12 at 13:48
  • Also related: http://stackoverflow.com/questions/2258891/how-to-access-the-private-variables-of-a-class-in-its-subclass – Wug Oct 09 '12 at 13:51

4 Answers4

1

You should always make the fields of a class private. And provide public accessor methods, to access those fields..

By doing this, you are achieving a level of encapsulation that is core to any OO language..

Also, other benefit of not allowing the access directly is that, you can modify your fields according to your need and security before letting the outer class access it..

In fact, it is generally suggested to access the private fields through getter methods even in the same class also.. '

This also helps the modification of your class easy..

Suppose, in future, you want to change the way your fields are accessed (That is you want to do some processing on the fields, before returning it).. Now, if you are accessing the fields directly even in your own class.. You would have to go and change at every place.. But if you used getter to access the fields, you would just need to modify your getter method..

public class A {  
    private int data;

    public int getData() {
        // Here apart from just return the `data`, you can perform some 
        // modification also according to your need..
    }
}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

It is a normal behaviour. A super class provides you an abstraction, you should deal with. YOu should not be botherd by implementation details. If tomorrow the implementation changes, you don't need to change your subclass - the super class hides it.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
0

What you are seeing is Encapsulation. You are controlling access to the members of the class via methods and not letting them being accessed directly.

Santosh
  • 17,667
  • 4
  • 54
  • 79
0

You might want to read up on the meaning of the terms public, protected and private in this instance.

A subclass cannot access fields and methods from its parent if those fields are private, if you wish a subclass to be able to access those fields and methods, then the visibility modifier you need to use (on the parent class), is protected.

The use of protected is specifically so subclasses and only subclasses can access fields and methods on a parent class.

Jon
  • 3,510
  • 6
  • 27
  • 32
  • "specifically so subclasses and only subclasses" Nope. In java, everything in your package can access your protected variables and functions. – Wug Oct 09 '12 at 13:38