Lec 05 - Encapsulations
Lec 05 - Encapsulations
Lecture 05
Encapsulation
09/07/2024
Bachelor of Science (Hons) in Computer Science | Software Engineering | Information Technology
Department of Computing
Faculty of Computing and Technology
Saegis Campus
Nugegoda
1
T.L.Navodi Sithara Saegis Campus
What is OOP
❑ OOP stands for Object-Oriented Programming.
❑ Procedural programming is about writing procedures or methods that perform operations on the data,
while object-oriented programming is about creating objects that contain both data and methods.
❑ As the name suggests, Object-Oriented Programming or Java OOPs concept refers to languages that use
objects in programming, they use objects as a primary source to implement what is to happen in the code.
Objects are seen by the viewer or user, performing tasks you assign.
3
T.L.Navodi Sithara Saegis Campus
Access Modifiers in Java
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.
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.
4
T.L.Navodi Sithara Saegis Campus
Understanding Java Access Modifiers
5
T.L.Navodi Sithara Saegis Campus
Encapsulation
• Encapsulation is one of the fundamental OOP concepts.
• Encapsulation is the technique of making the fields in a class private and providing
access to the fields via public methods.
6
T.L.Navodi Sithara Saegis Campus
Encapsulation
7
T.L.Navodi Sithara Saegis Campus
Encapsulation
• Encapsulation in Java is a mechanism of wrapping the data
(variables) and code acting on the data (methods) together as a
single unit.
8
T.L.Navodi Sithara Saegis Campus
Encapsulation
Achieving Encapsulation in Java
9
T.L.Navodi Sithara Saegis Campus
Encapsulation
• Encapsulation can be described as a protective barrier that prevents the code and data
being randomly accessed by other code defined outside the class.
• The main benefit of encapsulation is the ability to modify our implemented code
without breaking the code of others who use our code.
• With this feature Encapsulation gives maintainability, flexibility and extensibility to our
code.
• The get method returns the variable value, and the set method sets the value.
• Syntax for both is that they start with either get or set, followed by the name of the
variable, with the first letter in upper case:
10
T.L.Navodi Sithara Saegis Campus
Setters & Getters
11
T.L.Navodi Sithara Saegis Campus
Encapsulation
public String getId () {
public class Student
return id;
{
}
private String name;
public void setAge( int newAge){
private String id;
age = newAge;
private int age;
}
public void setName(String newName){
public int getAge() {
name = newName;
return age;
}
}
public void setId ( String newId){
public String getName() {
id = newId;
return name;
}
}
}
12
T.L.Navodi Sithara Saegis Campus
Encapsulation
public class TestEncap{
public static void main(String args[]) {
13
T.L.Navodi Sithara Saegis Campus
14
Lecturer Name Saegis Campus