Modifiers contains two types
The Access Modifiers specifies accessibility of a method, data method, class or constructor.
Access Modifiers in Java contains 4 types
Static, abstract, synchronized, native, volatile, transient etc are non access modifiers.
Private Access Modifier:
The Private Access Modifier is the Modifier that is accessible only within the class.
Default Access Modifier:
By default, it is treated as default when any modifier is not in use. Within package only default modifier is accessible.
Protected Access Modifier:
Through inheritance the Protected Access Modifier is accessible within the package and outside the package.
The Protected Access Modifier can’t be applied on the class. It can be applied only on the constructor, method and data member.
Public Access Modifier:
The Public Access Modifier has the widest scope among all the modifiers. It can be accessible everywhere.
Over ridden method must not be more restructive when if you are overriding any method.
Advantage of Java Package:
How to Access Package from another Package:
1. Using packagename.*
All the classes and interface will be accessible for packages but not for sub packages by using Using packagename.*
2. Using packagename.classname
Declared class of this package will only be accessible If you import package.classname.
3. Using fully qualified name
The declared class of this package will be accessible If you use fully qualified name only. Now import is not needed. But when you are accessing the class or interface you need to use fully qualified name every time. It is used when any two packages have the same class name
Sub Package in Java:
The package inside the package is said as Subpackage. To categorize the package further the subpackage should be created.
System, String, Reader, Writer, Socket etc are the classes of java package which is defined by the Sun Microsystem. In which there classes represents a particular group.
Eg :- For Input/ Output , Reader and Writer are the two classes. For networking, Socket and Server socket are the two classes etc and so on. Lang, Net, IO etc are the sub packages of Java Package which is sub categorized by sun, and put the Input/ Output related classes in IO package and Net package which contains server and server socket classes and so on.
How to put two Public Classes in a Package:
To, put two public classes in a package. Keep the package name same for two java source file containing one public class.