1

I am just now learning about Abstract classes in Java and I was wondering.
Would having package private fields be considered bad practices?
For example:

abstract class Parent {
    String testField;

    public void method() {
        System.out.println(testField);
    }
}

Then on the children class I would do

public final class Children extends Parent {

    @Override
    public void method() {
        logger.log(testField);
    }

}

In this example would it be bad practice?
Or should I make them private and instead use getters and setters for them like normally?

3
  • 1
    There have no ultimate single answer for these type of design questions. It's relative to the problem you are solving. Commented Mar 13, 2020 at 15:11
  • 1
    The Java standard library uses package private fields in lots of places. If you regard the innards of your package as implementation details, then it's up to you how you use it. Commented Mar 13, 2020 at 15:11
  • 2
    For the use case you have presented protected String testField; would be sufficient. Commented Mar 13, 2020 at 15:19

4 Answers 4

1

It depends on what you want to do. However, in many cases encapsulation or information hiding can be a useful principle. In your case, this would mean making the member variable protected or private and exposing it only through getters/setters or not at all. This yield some advantages:

  • You can change the implementation of your class (e.g. changing the type of testField) without breaking the code of other programmers
  • The code is more clear to other programmers as they only need to consider the public methods
  • It makes the code easier to test
  • It discourages feature envy and tight coupling
Sign up to request clarification or add additional context in comments.

Comments

1

There's lots of advice. Prefer composition to inheritance. Prefer interfaces to base classes. Keep type hierarchies flat. Class should tend be either leaf (could be marked final) or abstract.

The most important practice here is avoiding protected. It's a mistake in the language. Also avoid default/package private access on anything but top level types.

Comments

1

In general (meaning exceptions are allowed under well defined circumstances), a field of a class should be private, and any access to it will require the use of a getter or a setter (or, more general, an 'accessor' or a 'mutator').

One (well known) exception is for static final fields of an immutable or primitive type – sometimes referred to as a constant.

When you can make sure (really sure) that your code is the only one that will access that field (for now and all overseeable future), you may consider to make it public (rarely), package private or protected. You will find this pattern quite often in classes from the java and javax packages; it works there because you cannot place your new classes to one of these packages, and therefore the code in your class cannot access the package private and protected fields – and because the (abstract) base classes in these packages that have protected fields are itself not public, inheritance would not help.

It will not work in most other cases (although things are changing since Jigsaw …), so you are limited to private internal classes in these cases.

Of course, when you write only disposable code, considerations like these are obsolete. Otherwise, you should always completely encapsulate all data, so that you/your code has complete control over any access/modification of it.

Comments

0

We can make methods protected in base abstract classes so that only sub classes override it.

 protected void method() {
    System.out.println(testField);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.