Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

What is the difference in Java methods types?

What is the difference between a Private method, Public method, Protected method and a Package method?

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Private = only other methods within the same class may call it

    Public = any other class/method may call it

    Protected = only methods within the class or inheriting classes may call it

    Package = only classes/method within the same package may call it (rarely used)

    HTH

  • Anonymous
    5 years ago

    When I started out I didn't even recognize the constructor of a class, but it has the same name as the class without the word 'class' public class Car { // class members public int numWheels; // constructor public Car() { numWheels = 4; } // another constructor public Car( int total ) { numWheels = total; } OK, you recognize a method() by the parenthesis. method() means branch here, go seek the code with that name and follow directions. When completed, return to the line following the call(). However, constructors have to be called with the java keyword 'new' Car myCar = new Car(); Car mustang = new Car( 5 ); myCar will use the first constructor. mustang will use the 2nd constructor because it defines one argument to use it. the 'new' constructs the Object on the computer Heap (RAM). Once we have something in RAM, we can use the dot notation mustang.numWheels = 6; If I had made numWheels private private numWheels; then, I need 'getters' and 'setters' methods to manipulate or retrieve that value. the first time Objects made sense to me was a class of StopWatch. Every click of the mouse on the computer screen started a new box with a clock counting up. Each clock was unique with different times displayed counting up. the constructor(s) of those StopWatch class files made Objects. A right-click canceled the clocks one by one -- method(). The Object had a behavior. Which is what an Object can do: values <> states <> behaviors, but ONLY in RAM, not as a class file.

  • 1 decade ago

    The Four P’s of Protection

    Learning your four P’s (public, package, protected, and private) comes down to understanding the fundamental relationships that a method or variable within a class can have to the other classes in the system.

    public

    Because any class is an island unto itself, the first of these relationships builds on the distinction between the inside and the outside of the class. Any method or variable is visible to the class in which it is defined, but what if you want to make it visible to all the classes outside this class? The answer is obvious: simply declare the method or variable to have public access.

    Here are some examples of public declarations:

    public class APublicClass {

    public int aPublicInt;

    public String aPublicString;

    public float aPublicMethod() {

    . . .

    }

    }

    A variable or method with public access has the widest possible visibility. Anyone can see it. Anyone can use it. Of course, this may not always be what you want—which brings us to the next level of protection.

    package

    In C, there is the notion of hiding a name so that only the functions within a given source file can see it. In Java, source files are replaced by the more explicit notion of packages, which can group classes. For now, all you need to know is that the relationship you want to support is of a class to its fellow implementors of one piece of a system, library, or program (or to any other grouping of related classes). This defines the next level of increased protection and narrowed visibility. Due to an idiosyncrasy of the Java language, this next level of access has no precise name. It is indicated by the lack of any access modifier in a declaration. Historically, it has been called various suggestive names, including “friendly” and “package.” The latter usage seems most appropriate and is the one used here. Perhaps in a later release of the system, it will be possible to say package explicitly, but for now it is simply the default protection when none has been specified.

    protected

    The third relationship is between a class and its present and future subclasses. These subclasses are much closer to a parent class than to any other “outside” classes for the following reasons:

    1. Subclasses are usually more intimately aware of the internals of a parent class.

    2. Subclasses are often written by you or by someone to whom you’ve given your source code.

    3. Subclasses frequently need to modify or enhance the representation of the data within a parent class.

    No one else is allowed the privilege of this level of access; they must be content with the public

    face that the class presents.

    To support the level of intimacy reserved for subclasses, modern programming languages have invented an intermediate level of access between the previous two levels and full privacy. This level gives more protection and narrows visibility still further, but still allows subclasses full access. In Java, this level of protection is called, appropriately enough, protected:

    public class AProtectedClass {

    protected int aProtectedInt = 4;

    protected String aProtectedString = “and a 3 and a “;

    protected float aProtectedMethod() {

    . . .

    }

    }

    public class AProtectedClassSubclass extends AProtectedClass {

    public void testUse() {

    AProtectedClass aPC = new AProtectedClass();

    System.out.println(aPC.aProtectedString + aPC.aProtectedInt);

    aPC.aProtectedMethod(); // all of these are A.O.K.

    }

    }

    public class AnyClassInTheSamePackage {

    public void testUse() {

    AProtectedClass aPC = new AProtectedClass();

    System.out.println(aPC.aProtectedString + aPC.aProtectedInt);

    aPC.aProtectedMethod(); // NONE of these are legal

    }

    }

    Even though AnyClassInTheSamePackage is in the same package as AProtectedClass, it is not a subclass of it (it’s a subclass of Object). Only subclasses are allowed to see, and use, protected variables and methods

    private

    private is the most narrowly visible, highest level of protection that you can get— the diametric opposite of public. private methods and variables cannot be seen by any class other than the one in which they are defined:

    public class APrivateClass {

    private int aPrivateInt;

    private String aPrivateString;

    private float aPrivateMethod() {

    . . .

    }

    }

    This may seem extremely restrictive, but it is, in fact, a commonly used level of protection. Any private data, internal state, or representations unique to your implementation—anything that shouldn’t be directly shared with subclasses—is private.

Still have questions? Get your answers by asking now.