0

I noticed in my Java book, in the section about packages and the private modifier, that the code redundantly used private on the class and the members of the class being accessed outside of the package.

package bookpack;

public class Book {
    private String title;
    private String author;
    private int pubDate;

    public Book(String t, String a, int d) {
        title = t;
        author = a;
        pubDate = d;
    }

    public void show() {
        System.out.println(title);
        System.out.println(author);
        System.out.println(pubDate + "\n"); 
    }
}

When I remove the public from show(), Eclipse gives an error stating that the member cannot be accessed (when attempting to do so from another package). I understand that it is because it is not public and therefore cannot be accessed from outside the package. However, since the class is public, I thought that all members of the class would then be public, unless otherwise specified. That would follow the "general specifications here, specific specifications later" style, similar to inheritance. Much like how you cannot call a dynamic object from a static method. So why is the public tag required on the member of a public class? How does a public tag affect accessibility in the context of retrieving a public member of a class

Lightfire228
  • 546
  • 1
  • 5
  • 17
  • 4
    Nope. Access modifiers only affect the thing they are directly modifying. A `public class` means a `public class` only, not a `public class` and `public members`. I suppose this is at least partially also linked to the fact that package-private visibility is the default visibility in Java. That more follows the "limit visibility" idea for object-oriented programming. – awksp Jun 26 '14 at 04:01
  • Check [Controlling Access to Members of a Class](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) – Luiggi Mendoza Jun 26 '14 at 04:02
  • Welcome to Java. Your assumption[all members of the class would then be public, unless otherwise specified] is correct for C++ but not for java – Jabir Jun 26 '14 at 04:03
  • 1
    The lack of a modifier is already in use to mean "package access", so it cannot be used to mean "same as class". – Patricia Shanahan Jun 26 '14 at 04:05
  • So does Java require the class be `public` if one or more of its members are `public`, or can you forgo that by only tagging the members you need `public`? – Lightfire228 Jun 26 '14 at 04:12
  • Nope. Access modifiers are considered separately from each other. You can have a package-private class with public members. – awksp Jun 26 '14 at 04:13

2 Answers2

2

Expanded from the comments section

Access modifiers only apply to the things they directly modify. Thus, public on a class only affects the visibility of the class -- not the visibility of any of its members. Thus, you can provide public members for a package-private class, which could be useful if you have an abstract class you want to keep hidden from the public API.

In addition, the lack of a visibility modifier is already defined to mean package-private visibility. Thus, it cannot be used to mean "same as class". As for why the language is designed that way, the best I could come up with is that it might have seemed like a good balance between limiting visibility to the outside world while still allowing different top-level classes to interact.

awksp
  • 11,764
  • 4
  • 37
  • 44
1

As pointed out in comment

Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:

  • At the top level—public, or package-private (no explicit modifier).
  • At the member level—public, private, protected, or package-private (no explicit modifier).

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

For further details kindly go through following link

Jabir
  • 2,776
  • 1
  • 22
  • 31
  • I understand that, but the article mentions nothing of `public` in the context of both class and member. My only question was regarding the relationship between a `private/public` class and a `private/public` member of said class. However, user: user3580294 explained it thoroughly. – Lightfire228 Jun 26 '14 at 04:25