Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean for a method to be public/private/other in java? [closed]

Tags:

java

methods

What does it mean for a method to be public/private/other in java?
What are the advantages and disadvantages of these options?
What is my impetus, as someone trying to be a good programmer, to care?

like image 608
David Avatar asked Apr 15 '10 16:04

David


2 Answers

When a method is public it means it can be accessed by other objects

For instance:

class David {
     // public method, can be use by anyone
     public String getName() {
         return "David";
      }
 }

The method getName may be accessed by other classes because it is public:

 class Other  {
      David davidOne = new David();
      String davidsName = davidOne.getName(); //<-- compiles and runs
 }

The advantage.. well you can use it from other places.

When a method is private it means it can only be accessed by objects OF THE SAME CLASS

For instance, in this new definition:

class David {
     public String getName() {
         return "David";
     }
     // private method... nobody but David's "instances" can use it.. 
     private int getAge() {
         return 19;
     } 

 }

The method getAge can't be accessed by other classes because it is private, if you try to do it, the compiler will give you an error message:

 class Other  {
      David davidOne = new David();
      String davidsName = davidOne.getName(); 
      int davidsAge = davidOne.getAge(); //<-- Compiler error, getAge() is not visible
 }

But, if you can use it within David class:

class David {
     public String getName() {
         return "David";
     }
     // private method... nobody but David's "instance" can use it.. 
     private int getAge() {
         return 19;
     } 
     // Here the call to "getAge()" will succeed, because it is visible 
     // inside the class 
     public boolean hasSameAgeAs( David otherDavid ) {
         return this.getAge() == otherDavid.getAge();
     }
 }

The advantage? You can create a bunch of methods and keep them private, avoiding data corruption or in general preserving your objects encapsulated

About encapsulation

In OOP ( object oriented programming ) the intention is to model the software after real life objects.

Real life objects have ( among other things ) attributes and methods to access those attributes.

You want to make public some of those methods, and keep private others.

For instance, a Human being, have a heart. But it is not exposed to everybody, it would be dangerous. It is encapsulated inside our body.

If we were to model a software after a real Human we may declare the method: heartBeat as private ( so, nobody can access it )

In the other hand, it would be useful to have come public methods like getGender to find out if your Human instance is male or female.

There are other access modifiers such as: "protected" and package protected ( whose doesn't have a keyword )

 class David {
      // protected method
      protected int getBalance() { 
          return 1000000; 
      }
      // package protected or "default" method
      boolean knowsOop(){ 
          return true;
      }
 }

There the method getBalance can only be accesed by David instances and David subclasses ( create another thread for what is a subclass )

The method knowsOop can be accesses by anyone inside the package as David is defined.

Don't worry about this two access modifiers, they will make sense when you learn more about OOP and Java.

Finally you should really, really take time to read:

http://java.sun.com/docs/books/tutorial/java/javaOO/index.html

I hope this helps

like image 160
OscarRyz Avatar answered Oct 01 '22 13:10

OscarRyz


A public method can be accessed from everywhere, a private method only from the same class. The main advantage is the control over the API of an class. If I make only public what is needed, I can change the internal behaviour of a class , without breaking code depending on this class. You should care, because software changes often in the real world (at least it's my experience and others have it too) and the more every change breaks, the more energy you have to put into maintenance or the more bugs your software has. In the end it's a question of costs.

The possibility to hide internals of your class from users of this class to avoid breaking code by later changes is often called encapsulation or information hiding.

The two options besides public and private are package (without an modifier) and protected. The package-accessible method can also be accessed from within classes of the same package. I cannot remember to used that option in any useful way. protected methods can be accessed from classes, that inherit the class in question. That is often used to create classes with concrete behaviour for a defined API of the base-class. For example could you implement a new List-class by extending AbstractList and you only need to implement get and size (and one set-method for modifiable lists). The other methods exposed by the API of List are defined in the base-class, calling the three other methods if needed.

like image 30
Mnementh Avatar answered Oct 01 '22 14:10

Mnementh