Signed in as:
filler@godaddy.com
Signed in as:
filler@godaddy.com
A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Here, we will have the detailed learning of creating and using user-defined packages.
1) Java package is used to categorize the classes and interfaces so that they can be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
The package keyword is used to create a package in java.
If you are not using any IDE, you need to follow the syntax given below:
For example
The -d switch specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use . (dot).
ADAD
You need to use fully qualified name e.g. mypack.Simple etc to run the class.
To Compile: javac -d . Simple.javaTo Run: java mypack.Simple
Output:Welcome to package
The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder.
There are three ways to access the package from outside the package.
If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages.
The import keyword is used to make the classes and interface of another package accessible to the current package.
Output:Hello
If you import package.classname then only declared class of this package will be accessible.
Output:Hello
If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.
ADAD
It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class.
Output:Hello
If you import a package, all the classes and interface of that package will be imported excluding the classes and interfaces of the subpackages. Hence, you need to import the subpackage as well.
Package inside the package is called the subpackage. It should be created to categorize the package further.
Let's take an example, Sun Microsystem has definded a package named java that contains many classes like System, String, Reader, Writer, Socket etc. These classes represent a particular group e.g. Reader and Writer classes are for Input/Output operation, Socket and ServerSocket classes are for networking etc and so on. So, Sun has subcategorized the java package into subpackages such as lang, net, io etc. and put the Input/Output related classes in io package, Server and ServerSocket classes in net packages and so on.
To Compile: javac -d . Simple.javaTo Run: java com.javatpoint.core.Simple
Output:Hello subpackage
There is a scenario, I want to put the class file of A.java source file in classes folder of c: drive. For example:
ADAD
e:\sources> javac -d c:\classes Simple.java
To run this program from e:\source directory, you need to set classpath of the directory where the class file resides.e:\sources> set classpath=c:\classes;.;e:\sources> java mypack.Simple
The -classpath switch can be used with javac and java tool.
To run this program from e:\source directory, you can use -classpath switch of java that tells where to look for class file. For example:
e:\sources> java -classpath c:\classes mypack.Simple
Output:Welcome to package
There are two ways to load the class files temporary and permanent.
If you want to put two public classes in a package, have two java source files containing one public class, but keep the package name same. For example:
==============================================================================
There are two types of modifiers in Java: access modifiers and non-access modifiers.
The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it.
There are four types of Java access modifiers:
There are many non-access modifiers, such as static, abstract, synchronized, native, volatile, transient, etc. Here, we are going to learn the access modifiers only.
Let's understand the access modifiers in Java by a simple table.
Access Modifierwithin classwithin packageoutside package by subclass onlyoutside packagePrivateYNNNDefaultYYNNProtectedYYYNPublicYYYY
The private access modifier is accessible only within the class.
Simple example of private access modifier
In this example, we have created two classes A and Simple. A class contains private data member and private method. We are accessing these private members from outside the class, so there is a compile-time error.
If you make any class constructor private, you cannot create the instance of that class from outside the class. For example:
If you don't use any modifier, it is treated as default by default. The default modifier is accessible only within package. It cannot be accessed from outside the package. It provides more accessibility than private. But, it is more restrictive than protected, and public.
Example of default access modifier
In this example, we have created two packages pack and mypack. We are accessing the A class from outside its package, since A class is not public, so it cannot be accessed from outside the package.
In the above example, the scope of class A and its method msg() is default so it cannot be accessed from outside the package.
The protected access modifier is accessible within package and outside the package but through inheritance only.
The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class.
It provides more accessibility than the default modifer.
Example of protected access modifier
ADAD
In this example, we have created the two packages pack and mypack. The A class of pack package is public, so can be accessed from outside the package. But msg method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.
Output:Hello
The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.
Example of public access modifier
Output:Hello
If you are overriding any method, overridden method (i.e. declared in subclass) must not be more restrictive.
The default modifier is more restrictive than protected. That is why, there is a compile-time error.
==============================================================================
Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines.
We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.
The Java Bean class is the example of a fully encapsulated class.
By providing only a setter or getter method, you can make the class read-only or write-only. In other words, you can skip the getter or setter methods.
It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method. You can write the logic not to store the negative numbers in the setter methods.
It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members.
The encapsulate class is easy to test. So, it is better for unit testing.
The standard IDE's are providing the facility to generate the getters and setters. So, it is easy and fast to create an encapsulated class in Java.
Let's see the simple example of encapsulation that has only one field with its setter and getter methods.
File: Student.java
File: Test.java
Compile By: javac -d . Test.java
Run By: java com.javatpoint.Test
Output:
vijay
Now, you can't change the value of the college data member which is "AKG".
Now, you can't get the value of the college, you can only change the value of college data member.
Let's see another example of encapsulation that has only four fields with its setter and getter methods.
File: Account.java
File: TestAccount.java
ADAD
Test it Now
Output:
7560504000 Sonoo Jaiswal sonoojaiswal@javatpoint.com 500000.0
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.