05 Java Encapsulation
05 Java Encapsulation
PROGRAMMING
SUBJECT DESCRIPTION: JAVA ENTERPRISE EDITION PROGRAMMING
SUBJECT CODE: CS220
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.
Simple example of java package
The package keyword is used to create a
package in java.
How to compile java package
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).
How to run java package program
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.
How to access package from another package?
There are three ways to access the package from outside the package.
1.import package.*;
2.import package.classname;
3.fully qualified name.
1) Using packagename.*
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.
Example of package that import the
packagename.*
2) Using packagename.classname
If you import package.classname then only declared class of this package will be
accessible.
Example of package by import package.classname
3) Using fully qualified name
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.
It is generally used when two packages have same class name e.g. java.util and
java.sql packages contain Date class.
Example of Subpackage
• access modifiers
• 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.
FOUR TYPES OF JAVA ACCESS MODIFIERS
1.Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
2.Default: The access level of a default modifier is only within the package. It cannot
be accessed from outside the package. If you do not specify any access level, it will
be the default.
3.Protected: The access level of a protected modifier is within the package and
outside the package through child class. If you do not make the child class, it cannot
be accessed from outside the package.
4.Public: The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.
There are many non-access modifiers, such as
static, abstract, synchronized, native, volatile,
transient, etc.
UNDERSTANDING JAVA ACCESS
MODIFIERS
1) PRIVATEThe 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.
ROLE OF PRIVATE CONSTRUCTOR
If you make any class constructor private, you cannot create the instance of that
class from outside the class. For example:
The default modifier is more restrictive than protected. That is why, there is a
compile-time error.
ENCAPSULATION IN JAVA
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.
ADVANTAGE OF ENCAPSULATION IN JAVA
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.
SIMPLE EXAMPLE OF ENCAPSULATION IN
JAVA
Let's see the simple example of encapsulation that has only one field with its setter
and getter methods.
File: Student.java
READ-ONLY CLASS