9

my question is concerning about the "protected" access modifier.

I know its functionality, but I don't know when I need to use it. Conceptually methods in a class could be divided as: constructors setters/getters methods used from clients (i.e other classes) internal methods (used from other methods in the class)

ThiepLV
  • 1,219
  • 3
  • 10
  • 21

7 Answers7

20

You use protected when

  • Your class is designed for inheritance - You expect the users of your library to inherit from the class that you are designing. Very often the class will be abstract.
  • The class provides special functionality to its derived classes that must not be visible to other classes - You know that derived classes must have access to information that would otherwise be private, or
  • The derived classes must provide functionality to the base class - See Template Method Pattern for information about this use of protected methods.

Note that protected methods are similar to public methods in the sense that once you put them in, they need to stay in for as long as you support your library. Unlike private methods that you can freely remove, protected methods remain a part of the interface of your class.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
14

Use it when you need to do some internal stuff that is not exposed in public API but still needs to be overriden by subclasses.

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
6

You need to use the protected access modifier, when you want the descendant class to see the fields / methods of the super class, BUT you do not want other classes to see these.

Peter Jaloveczki
  • 2,039
  • 1
  • 19
  • 35
4

One situation I've found it useful in is when a superclass's method is intended to call a subclass's method, e.g.

public abstract class SuperClass
    public final void exposedMethod {
        hiddenMethod();
    }

    abstract protected void hiddenMethod();
}

public class SubClass extends SuperClass {
    protected void hiddenMethod() { }
}

In this case exposedMethod takes care of things like logging and retry logic, while hiddenMethod is the actual method implementation

xlm
  • 6,854
  • 14
  • 53
  • 55
Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69
  • So different subclasses use the same method name for different things? Just like using different paint() methods between applets. – huseyin tugrul buyukisik Jul 11 '13 at 13:58
  • @huseyin tugrul buyukisik Or in one case `exposedMethod` would remove a message from a queue and pass it to `hiddenMethod`, and each subclass had a different way of processing the message; then `exposedMethod` would perform some cleanup operations on the queue - the concern was that the people implementing `hiddenMethod` would forget to perform the cleanup operations, so these were moved to a final method in the superclass – Zim-Zam O'Pootertoot Jul 11 '13 at 14:02
  • Can it see local variables of exposedMEthod? – huseyin tugrul buyukisik Jul 11 '13 at 14:05
  • @huseyin tugrul buyukisik No, those would need to be passed in as parameters, although they can see protected variables in `SuperClass` which can be set in `exposedMethod` – Zim-Zam O'Pootertoot Jul 11 '13 at 14:09
  • P'Pootertoot Okay, thanks. – huseyin tugrul buyukisik Jul 11 '13 at 14:10
2

A primary usage of protected methods is when a class expects its derived classes to override the method with its own features, and also call the base class version.

E.g.

public class Base
{
    public void doStuff()
    {
        a(); b(); c();
    }

    protected void a(){ //does something 
    }
    protected void b(){ //does something 
    }
    protected void c(){ //does something 
    }
}

...

public class Derived extends Base
{
    protected void b()
    {
        // Does something different before the original functionality.
        super.b(); // Calls the original functionality
        // Does something different after the original functionality.
    }
}

...

public class Main
{
    public static void main(String args[])
    {
        Base b = new Derived();
        b.doStuff(); // Calls b.a(), ((Derived)b).b(), b.c()
    }
}
Kaz Dragon
  • 6,681
  • 2
  • 36
  • 45
2

A lot of classes in the java api use protected constructers so that you can only have instances of objects from other objects. Example: The Graphics class. It has a protected constructor, and a way to obtain a copy of the Graphics class is to have a Image object to call getGraphics() on.

user2097804
  • 1,122
  • 4
  • 13
  • 22
1

You should always encapsulate your code to restrict access to the meet the exact level of access needed. Use the protected modifier when you need to only allow access to the code within the package or when its subclassed. I don't get what you mean by...

Conceptually methods in a class could be divided as: constructors setters/getters methods used from clients (i.e other classes) internal methods (used from other methods in the class)

Just put the access modifier infront of what you need to be labeled as "protected".

dghalbr
  • 109
  • 1
  • 12