lecture 12 Encapsulation (1)
lecture 12 Encapsulation (1)
Encapsulation
Package, Access Modifiers, attributes visibility: public, private,
protected , getters & setters
• Here, we will have the detailed learning of creating and using user-defined
packages.
2
Java Package
3
Simple example of java package
• The package keyword is used to create a package in java.
//save as Simple.java
package mypack;
Output:
Welcome to package
4
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.
5
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.*
//save by A.java
package pack;
public class A{
public void msg(){
System.out.println("Hello");
}
}
• Output:
//save by B.java
package mypack; – Hello
import pack.*; // or you can write import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
} 6
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.
7
Access Modifiers
8
Encapsulation
• Encapsulation is one of the four fundamental OOP concepts. The other
three are inheritance, polymorphism, and abstraction.
• 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.
9
Encapsulation
• Another way to think about encapsulation is, it is a protective shield that
prevents the data from being accessed by the code outside this shield.
– As in encapsulation, the data in a class is hidden from other classes using the
data hiding concept which is achieved by making the members or methods of a
class private, and the class is exposed to the end-user or the world without
providing any details behind implementation using the abstraction concept, so it
is also known as a combination of data-hiding and abstraction.
10
Example how to achieve Encapsulation in Java
/* File name : EncapTest.java */
public class EncapTest {
private String name;
private String idNum;
private int age;
• The public setXXX() and getXXX() methods are the access points of the instance
variables of the EncapTest class. Normally, these methods are referred as getters
and setters. Therefore, any class that wants to access the variables should access
them through these getters and setters.
• The variables of the EncapTest class can be accessed using the following program
• Output
– Name : James Age : 20
12
Advantage of Encapsulation in Java
• Data Hiding: The user will have no idea about the inner implementation of
the class. It will not be visible to the user how the class is storing values in
the variables. The user will only know that we are passing the values to a
setter method and variables are getting initialized with that value.
• Testing code is easy: Encapsulated code is easy to test for unit testing.
13
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
class Account {
private long acc_no;
private String name;
private float amount;
• File: TestAccount.java
15