Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Encapsulation

Java Encapsulation


In object-oriented programming, encapsulation refers to a method of packaging and hiding the implementation details of an abstract functional interface.

Encapsulation can be thought of as a protective barrier that prevents the code and data of a class from being randomly accessed by code defined in external classes.

To access the code and data of the class, you must go through strict interface control.

The main function of encapsulation is that we can modify our own implementation code without modifying the program fragments that call our code.

Proper encapsulation makes the code easier to understand and maintain, and also enhances code security.

Advantages of Encapsulation


Steps to Implement Java Encapsulation

  1. Modify the visibility of attributes to restrict access to them (usually restricted to private), for example:

public

class

Person

{

private

String

name

;

private

int

age

;

}

In this code, the name and age attribute is set to private, so only this class can access it; other classes cannot, thus hiding the information.

  1. Provide public method access to each value attribute from the outside, that is, create a pair of setter and getter methods for accessing the private attribute, for example:

public

class

Person

{

private

String

name

;

private

int

age

; ​

public

int

getAge

(

)

{

return

age

;

}

​

public

String

getName

(

)

{

return

name

;

}

​

public

void

setAge

(

int

age

)

{

this

.

age

=

age

;

}

​

public

void

setName

(

String

name

)

{

this

.

name

=

name

;

}

}

The use of this keyword is to resolve the naming conflict between the instance variable (private String name) and the local variable (the name variable in setName(String name)).


Example

Let's look at an example of a Java encapsulation class:

EncapTest.java file code:

/*

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

;

}

}

In the above example, the public methods are the entry points for external classes to access the member variables of this class.

Usually, these methods are called getter and setter methods.

Therefore, any class that wants to access the private member variables of a class must go through these getter and setter methods.

The following example illustrates how the variables of the EncapTest class are accessed:

RunEncap.java file code:

/*

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

(

)

)

;

}

}

The compilation and execution results of the above code are as follows:


Name : James Age : 20