Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When a method is <strong>public</strong> it means it can be accessed by other objects </p> <p>For instance:</p> <pre><code>class David { // public method, can be use by anyone public String getName() { return "David"; } } </code></pre> <p>The method <code>getName</code> may be accessed by other classes because it is public:</p> <pre><code> class Other { David davidOne = new David(); String davidsName = davidOne.getName(); //&lt;-- compiles and runs } </code></pre> <p>The advantage.. well you can use it from other places.</p> <p>When a method is <strong>private</strong> it means it can only be accessed by objects <strong>OF THE SAME CLASS</strong> </p> <p>For instance, in this new definition:</p> <pre><code>class David { public String getName() { return "David"; } // private method... nobody but David's "instances" can use it.. private int getAge() { return 19; } } </code></pre> <p>The method <code>getAge</code> 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:</p> <pre><code> class Other { David davidOne = new David(); String davidsName = davidOne.getName(); int davidsAge = davidOne.getAge(); //&lt;-- Compiler error, getAge() is not visible } </code></pre> <p>But, if you can use it <strong>within</strong> David class:</p> <pre><code>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(); } } </code></pre> <p>The advantage? You can create a bunch of methods and keep them private, avoiding data corruption or in general preserving your objects <a href="http://en.wikipedia.org/wiki/Object_oriented_programming#Encapsulation" rel="noreferrer">encapsulated</a> </p> <p><strong>About encapsulation</strong></p> <p>In <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" rel="noreferrer">OOP</a> ( object oriented programming ) the intention is to model the software after real life objects. </p> <p>Real life objects have ( among other things ) attributes and methods to access those attributes. </p> <p>You want to make public some of those methods, and keep private others. </p> <p>For instance, a <strong>Human</strong> being, have a heart. But it is not exposed to everybody, it would be dangerous. It is <strong>encapsulated</strong> inside our body. </p> <p>If we were to model a software after a real <strong>Human</strong> we may declare the method: <code>heartBeat</code> as private ( so, nobody can access it ) </p> <p>In the other hand, it would be useful to have come <strong>public</strong> methods like <code>getGender</code> to find out if your <strong>Human</strong> instance is male or female. </p> <p>There are other access modifiers such as: "protected" and package protected ( whose doesn't have a keyword ) </p> <pre><code> class David { // protected method protected int getBalance() { return 1000000; } // package protected or "default" method boolean knowsOop(){ return true; } } </code></pre> <p>There the method <code>getBalance</code> can only be accesed by <code>David</code> instances and <code>David</code> subclasses ( create another thread for what is a subclass ) </p> <p>The method <code>knowsOop</code> can be accesses by anyone inside the package as David is defined. </p> <p>Don't worry about this two access modifiers, they will make sense when you learn more about OOP and Java. </p> <p>Finally you should really, really take time to read:</p> <p><a href="http://java.sun.com/docs/books/tutorial/java/javaOO/index.html" rel="noreferrer">http://java.sun.com/docs/books/tutorial/java/javaOO/index.html</a></p> <p>I hope this helps</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload