Iv. Lesson Proper: Applications Development and Emerging Technologies
Iv. Lesson Proper: Applications Development and Emerging Technologies
What are the basic concepts of Encapsulation as feature of OOP and how to use it?
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;
}
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
The variables of the EncapTest class can be accessed using the following program −
/* File name : RunEncap.java */
public class RunEncap {
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”.