java - Public class requires public members (when accessing members)? -
i noticed in java book, in section packages , private modifier, code redundantly used private
on class , members of class being accessed outside of 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 remove public
show()
, eclipse gives error stating member cannot accessed (when attempting package). understand because not public
, therefore cannot accessed outside package. however, since class public, thought members of class public
, unless otherwise specified. follow "general specifications here, specific specifications later" style, similar inheritance. how cannot call dynamic object static method. why public
tag required on member of public class? how public
tag affect accessibility in context of retrieving public member of class
expanded comments section
access modifiers apply things directly modify. thus, public
on class affects visibility of class -- not visibility of of members. thus, can provide public
members package-private class, useful if have abstract class want keep hidden public api.
in addition, lack of visibility modifier defined mean package-private visibility. thus, cannot used mean "same class". why language designed way, best come might have seemed balance between limiting visibility outside world while still allowing different top-level classes interact.
Comments
Post a Comment