Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

05 Java Encapsulation

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

05 Java Encapsulation

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

JAVA ENTERPRISE EDITION

PROGRAMMING
SUBJECT DESCRIPTION: JAVA ENTERPRISE EDITION PROGRAMMING
SUBJECT CODE: CS220

ENGR. RUELLA YARES MAGTAAS, CPE


JAVA PACKAGE
Java Package

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.
Advantage of Java Package

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

You need to use fully qualified name e.g.


mypack.Simple etc to run the class.

To Compile: javac -d . Simple.java


To Run: java mypack.Simple

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 package by import fully qualified name

If you import a package, subpackages will not be imported.


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.
Sequence of the program must be package then import then class.
Subpackage in java

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.

Example of Subpackage

The standard of defining package is domain.company.package e.g.


com.javatpoint.bean or org.sssit.dao.
How to send the class file to another directory or
drive?
There is a scenario, I want to put the class file of A.java source file in classes folder of
c: drive. For example:
To Compile:
e:\sources> javac -d c:\classes Simple.java
To Run:

Another way to run this program by -classpath switch of java:


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
Ways to load the class files or jar files
•Temporary
•By setting the classpath in the command prompt
•By -classpath switch
•Permanent
•By setting the classpath in the environment variables
•By creating the jar file, that contains all the class files, and copying the jar file in the jre/lib/ext folder.

There can be only one public class in a java source


file and it must be saved by the public class name.
HOW TO PUT TWO PUBLIC CLASSES IN A
PACKAGE?
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:
ACCESS MODIFIERS IN JAVA
TWO TYPES OF MODIFIERS IN JAVA

• 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:

A class cannot be private or protected except


nested class.
If you don't use any modifier, it is treated as default by default.
The default modifier is accessible only within package. It cannot
2) DEFAULT 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
3) PROTECTED
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


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.
4) PUBLIC
The public access modifier is accessible everywhere. It has the
widest scope among all other modifiers.
Example of public access modifier
JAVA ACCESS MODIFIERS WITH METHOD
OVERRIDING
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
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

Now, you can't change the value of the college


data member which is "AKG".
WRITE-ONLY CLASS
ANOTHER EXAMPLE OF ENCAPSULATION
IN JAVA
Let's see another example of encapsulation that has only four fields with its setter
and getter methods.
File: Account.java

You might also like