If we can access private data members using Accessors than why cant we access private methods?

1k views Asked by At

We can access Private data members using accessor method such as

 private int num=5;

 public int get_num()
 {
  return num;
 }

and we can access the returned value from any class even though num is private data member.

So on Similar note cant we create accessor method that returns private methods? I just had a thought about it,If we cannot do this please explain.Thank You

4

There are 4 answers

0
ezaquarii On BEST ANSWER

You can access private method via public method. This is sometimes used to wrap complicated private method and expose simpler, public API.

class Delegator {

    private void doPrivateStuff(int param) { ... }

    public void doStuffOnce() {
        doPrivateStuff(1);
    }

    public void doStuffIfConditionIsMet() {
        if(condition) {
            doPrivateStuff(1);
        }
    }
}

You can also access private methods using reflection. http://tutorials.jenkov.com/java-reflection/private-fields-and-methods.html

0
Avinash Anand On

Private methods are created when refactoring your code. They are implementation details, which nobody outside needs to know. They are used inside public methods, which are supposed to provide the functionality you want to provide to your client(each public method can be called an API which is used/consumed by other classes i.e. its clients).

Access modifiers are provided to help you achieve proper abstraction. So anything marked private is directly accessible only inside your class. But if you want someone outside to read/write its value, you expose it via getters/setters. Similarly private methods are not accessible outside the class. But nobody is stopping you from creating public method that only calls this private method. This way you do achieve access to private method. But it would be a very poor design.

0
Carcigenicate On

Because the accessor is public, it can be accessed from outside the class; even if it returns private data.

The access modifier of the returned data isn't relevant, only the access modifier of the method matters.

If you created a public method that returned a reference to a private method, yes, you could then call the private method from outside the class. That would entirely defeat the purpose of making the method private however.

You my be wondering "why make a public getter to a private variable". The answer is that although it's possible to retrieve the private data, it isn't possible (unless you create a public setter as well) to change the public data. This assumes that the private days is immutable. If it's mutable, retuning a reference to private data negates any protection you get from making the variable private.

2
Michael Lihs On

TL;DR

Accessing private methods violates the basic object oriented principle of encapsulation. The question is not "can we access a private method?" but "should we ever do it?". It contradicts the principle of information hiding and it breaks the contract an object "offers" to its consumers. Objects or classes that enforce you to do that should be considered poorly designed.

Why you shouldn't access private members

First a classes should implement a part of your domain logic that is so closely related that there are little interactions necessary with the outside world to fulfill its duties (this is called high cohesion). You then define a public interface for your class / object with functionality that you expose to the outside world - consumers of your object must only use this interface (this is called loose coupling).

Private methods can be seen as a way to structure your code inside your class in a better readable way - they are not intended to be shared with the outside world. Consider them as a "safe space" for the developer of the class to make arbitrary changes without breaking the contract. That's the reason why there can be bad side effects if you actually access private methods: your code is likely to break it the developer of the class decides to change the implementation of such a method. Effective Java, Chapter 4, Item 13 explains this for protected members:

For members of public classes, a huge increase in accessibility occurs when the access level goes from package-private to protected. A protected member is part of the class's exported API and must be supported forever. Also, a protected member of an exported class represents a public commitment to an implementation detail.

Exceptions

The only exception I know from the rule of "not accessing private members outside of an object" is in testing, when you either want to reset a certain variable (e.g. a Singleton) or you want to facilitate the testing of complex logic by testing the private parts of the implementation and hence reducing complexity in testing.