0% found this document useful (0 votes)
12 views14 pages

Lec 05 - Encapsulations

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)
12 views14 pages

Lec 05 - Encapsulations

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/ 14

CS SE 1209 & IT 1210

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.

❑ Object-oriented programming has several advantages over procedural programming:

• OOP is faster and easier to execute


• OOP provides a clear structure for the programs
• OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes the code easier to maintain,
modify and debug
• OOP makes it possible to create full reusable applications with less code and shorter development time
2
T.L.Navodi Sithara Saegis Campus
What is OOP

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.

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.

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.

• If a field is declared private, it cannot be accessed by anyone outside the class,


thereby hiding the fields within the class.

• For this reason, encapsulation is also referred to as data hiding.

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.

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

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[]) {

Student student1 = new Student();


student1.setName(“ABC");
student1.setAge(23);
student1.setId(“Saegis5210");

System.out.println("Name : " + student1.getName()+


" Age : "+ student1.getAge()+ " Id : "+ student1.getId() );
}
}

13
T.L.Navodi Sithara Saegis Campus
14
Lecturer Name Saegis Campus

You might also like