简体   繁体   中英

Java: Easy way to make protected methods public

I have a class (Capsule) which has a lot of methods (30+) that are protected. The idea is to allow devs to extend this class and use the protected methods within the class (ImADev), but leave it up to the dev to expose them as public (Overriding the methods as they see fit).

I now have a use case where I want to pass these objects into a few Utility classes/methods and allow them to have access to the protected methods .. except they're protected, so my utility classes/services can't see them.

See the example below:

// My Capsule class with protected methods
public abstract class Capsule {
   public Capsule(..) { .. }
   protected String getFoo() { return "foo"; }
}

// How a dev should use the class
public class ImADev extends Capsule {
    public ImADev(..) { super(..); }
    public String getBar() { this.getFoo() + "BAR");
}

// A special utility class
// Helper.helper() can accept any class derived from Capsule
public class Helper {
    public String helper(Capsule c) { 
        return " im doing something awesome with " + c.getFoo();
        // Except i cant do this, because c.getFoo() is protected.
    } 
}

What's the best way around this? Keep in mind I have a lot of protected methods.

Either your class is doing too much, in which case break it up into smaller more well defined classes

Or, implement safe public methods that your utility class can call to wrap you protected methods

Or, just make them public because it sounds like they might/should be

Or, separate the data in the class from the behaviour (composition) then your utility class only has to deal with safer public getter methods an not the protected behaviour methods that can change the internals

A bit difficult to provide a better answer without knowing more about your application and it's use

There is no way 'around' this, except to put the utility class in the same package as the classes with the protected method. If those classes are in more then one package, that won't help.

In deciding to use protected, you made a decision, and now you are stuck with the implications. You can live with it, change to public, or change package membership to take advantage of default access.

I see at least two options.

  1. Make Helper a subclass of Capsule. Or,
  2. Turn your protected methods into default accessibility, and put the Helper class in the same package as the Capsule class.

Edit: You can also probably accomplish exactly what you want with reflection.

Actually there is a way to call protected methods from outside the package or subclass: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/AccessibleObject.html

I'm just kidding! This tool exists for technical stuff like serialization of persistance.

Declaring a method as "protected" is just a way to organise your code and define which part should deal which which data, so trying to get "around" is not a good idea, unless you mistakenly declared it protected. In this case correct the first mistake.

If you are interested in how to organize your code in an efficient and clear way, i highly recommend reading http://java.sun.com/docs/books/effective/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM