Lightfire228
Lightfire228

Reputation: 616

Public class requires public members (when accessing members)?

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

Upvotes: 0

Views: 1056

Answers (2)

awksp
awksp

Reputation: 11867

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.

Upvotes: 2

Jabir
Jabir

Reputation: 2866

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

Upvotes: 1

Related Questions