<aside> π‘ Java has an organizational unit called package.
</aside>
Can use
import
to use packages directly:import java.math.BigDecimal
To import all classes in the java.math
package:
import java.math.*
β *
denotes all.
<aside>
π‘ Note that *
is not recursive.
β Cannot write : import java.*
β Will NOT import all packages in java
.
</aside>
The naming convention for packages in java is similar to Internet Domain name, but in reverse:
- Internet Domain:
onlinedegree.iitm.ac.in
- Package Name:
in.ac.iitm.onlinedegree
Add a package header to include a class in a packge:
package in.ac.iitm.onlinedegree; public class Employee { ... } // This class is now available in the above defined package.
<aside> π‘ By default, all classes in a directory belong to the same anonymous package.
</aside>
public
and private
<aside> π‘ If we donβt specify modifiers, the default visibility is public within the package. (Applies to both methods and variables)
</aside>
We can also restrict visibility with respect to the class inheritance hierarchy β i.e, visibile to subclasses only.
- Use
protected
modifier β Means that it is visible within the subtree β All subclasses.