0% found this document useful (0 votes)
33 views15 pages

lecture 12 Encapsulation (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views15 pages

lecture 12 Encapsulation (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Lecture 12:

Encapsulation
Package, Access Modifiers, attributes visibility: public, private,
protected , getters & setters

Object Oriented Programming


1
Introduction
• 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.

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

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;

public class Simple{


public static void main(String args[]){
System.out.println("Welcome to package");
}
}

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.

• There are four types of Java access modifiers:


– Private: The access level of a private modifier is only within the class. It cannot
be accessed from outside the class.
– 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.
– 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.
– 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.

7
Access Modifiers

8
Encapsulation
• Encapsulation is one of the four fundamental OOP concepts. The other
three are inheritance, polymorphism, and abstraction.

• Encapsulation in Java is a mechanism of wrapping the data (variables)


and code acting on the data (methods) together as a single unit. In
encapsulation, the variables of a class will be hidden from other classes,
and can be accessed only through the methods of their current class.
Therefore, it is also known as data hiding.

• To achieve encapsulation in Java −


– Declare the variables of a class as private.
– Provide public setter and getter methods to modify and view the variables
values.

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

– Technically in encapsulation, the variables or data of a class is hidden from any


other class and can be accessed only through any member function of its own
class in which it is declared.

– 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.

– Encapsulation can be achieved by Declaring all the variables in the class as


private and writing public methods in the class to set and get the values of
variables

10
Example how to achieve Encapsulation in Java
/* File name : EncapTest.java */
public class EncapTest {
private String name;
private String idNum;
private int age;

public int getAge() {


return age;
}
public String getName() {
return name;
}
public String getIdNum() {
return idNum;
}
public void setAge( int newAge) {
age = newAge;
}
public void setName(String newName) {
name = newName;
}
public void setIdNum( String newId) {
idNum = newId;
} 11
}
Example how to achieve Encapsulation in Java

• 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

/* File name : RunEncap.java */


public class RunEncap {

public static void main(String args[]) {


EncapTest encap = new EncapTest();
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");

System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge());


}
}

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

• Increased Flexibility: We can make the variables of the class as read-


only or write-know that only depending on our requirement. If we wish to
make the variables read-only then we have to omit the setter methods like
setName(), setAge(), etc. from the above program or if we wish to make
the variables as write-only then we have to omit the get methods like
getName(), getAge(), etc. from the above program

• Reusability: Encapsulation also improves the re-usability and easy to


change with new requirements.

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

public long getAcc_no() {


return acc_no; //756005004
}
public void setAcc_no(long acc_no) {
this.acc_no = acc_no; //756005004
}
public String getName() {
return name; // Benjamin Gates
}
public void setName(String name) {
this.name = name; /// Benjamin Gates
}
public float getAmount() {
return amount; //500000
}
public void setAmount(float amount) {
this.amount = amount; //500000
} 14
}
Another Example of Encapsulation in Java

• File: TestAccount.java

//A Java class to test the encapsulated class Account.


public class TestEncapsulation {
public static void main(String[] args) {
//creating instance of Account class
Account acc=new Account();

//setting values through setter methods


acc.setAcc_no(7560504000L);
acc.setName(“Benjamin Gates");
acc.setAmount(500000f);

//getting values through getter methods


System.out.println(acc.getAcc_no()+" "+acc.getName()+" "+ acc.getAmount());
}
}
• Output:
– 7560504000 Benjamin Gates 500000.0

15

You might also like