0% found this document useful (0 votes)
52 views4 pages

Iv. Lesson Proper: Applications Development and Emerging Technologies

Encapsulation is one of the four fundamental OOP concepts that involves wrapping code and data together in a class. It provides data hiding by making class variables private and providing public getter and setter methods to access and modify the variable values. Getter methods return private variable values without allowing modification while setter methods allow modification by accepting parameters and assigning them to private variables. Encapsulation benefits include making fields read-only or write-only and giving the class full control over stored data.

Uploaded by

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

Iv. Lesson Proper: Applications Development and Emerging Technologies

Encapsulation is one of the four fundamental OOP concepts that involves wrapping code and data together in a class. It provides data hiding by making class variables private and providing public getter and setter methods to access and modify the variable values. Getter methods return private variable values without allowing modification while setter methods allow modification by accepting parameters and assigning them to private variables. Encapsulation benefits include making fields read-only or write-only and giving the class full control over stored data.

Uploaded by

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

Applications Development and Emerging Technologies Page 1 of 4

What are the basic concepts of Encapsulation as feature of OOP and how to use it?

What are the Basic Concepts of Encapsulation as feature of


OOP and how to used it?

IV. LESSON PROPER

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.
Example
Following is an example that demonstrates 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) {
Applications Development and Emerging Technologies Page 2 of 4
What are the basic concepts of Encapsulation as feature of OOP and how to use it?

name = newName;
}

public void setIdNum( String newId) {


idNum = newId;
}
}

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.

Accessors

An Accessor method is commonly known as a get method or simply a getter. A property of the object is
returned by the accessor method. They are declared as public. A naming scheme is followed by
accessors, in other words they add a word to get in the start of the method name. They are used to
return the value of a private field. The same data type is returned by these methods depending on their
private field.

Syntax

1. public data type get___()  
2. {  
3.   return data type;  
4. }  

Mutators

A Mutator method is commonly known as a set method or simply a setter. A Mutator method
mutates things, in other words change things. It shows us the principle of encapsulation. They are
also known as modifiers. They are easily spotted because they started with the word set. They are
declared as public. Mutator methods do not have any return type and they also accept a parameter
of the same data type depending on their private field. After that it is used to set the value of the
private field.

Syntax

1. public void set ____(data type argument) {  


2.     this.instance variable = argument;  
3. }  
Applications Development and Emerging Technologies Page 3 of 4
What are the basic concepts of Encapsulation as feature of OOP and how to use it?

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());
}
}
This will produce the following result −
Output
Name : James Age : 20

Benefits of Encapsulation
 The fields of a class can be made read-only or write-only.
 A class can have total control over what is stored in its fields.
Inheritance can be defined as the process where one class acquires the properties (methods and
fields) of another. With the use of inheritance the information is made manageable in a hierarchical
order.
The class which inherits the properties of other is known as subclass (derived class, child class) and
the class whose properties are inherited is known as superclass (base class, parent class).
Applications Development and Emerging Technologies Page 4 of 4
What are the basic concepts of Encapsulation as feature of OOP and how to use it?

EXERCISE 1

A student is to create a simple Java program about student enrolment that will classify regular and
irregular students. The student type is either ‘R’ for regular and ‘I’ for Irregular. What could be the name
of your get and set methods in the super class besides the main() method in the program?

EXERCISE 2

Your school would like to create simple registration system to help students enrolled in the college.
Create a get method that will return the type of the student, “regular” and “irregular” and the input is
student code, ‘I’ for “irregular and ‘R’ for “regular”.

You might also like