0% found this document useful (0 votes)
11 views9 pages

LESSON 8 Encapsulation DIIT PPT DCIT 50 OOP

Encapsulation involves binding object state and behavior together within a class. It hides implementation details and forces users to access data through an interface of getter and setter methods. The document discusses encapsulation concepts like data hiding, advantages of encapsulation, and how to implement it in Java using private fields and public accessor methods. It also describes common access modifiers like public, private, protected that define accessibility of class members from within and outside the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

LESSON 8 Encapsulation DIIT PPT DCIT 50 OOP

Encapsulation involves binding object state and behavior together within a class. It hides implementation details and forces users to access data through an interface of getter and setter methods. The document discusses encapsulation concepts like data hiding, advantages of encapsulation, and how to implement it in Java using private fields and public accessor methods. It also describes common access modifiers like public, private, protected that define accessibility of class members from within and outside the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Industrial and

Information Technology

DCIT 50: Object Oriented Programming

LESSON
Encapsulation

Prepared:

NATHANAEL F. BUENO
Department of Industrial and
Information Technology

Content
• Concept of Encapsulation
• Understand when to use access modifiers in designing a class.
Department of Industrial and
Information Technology

Encapsulation
Encapsulate – ‘to enclose in or as if in a capsule
- Merriam-Webster
Encapsulation simply means binding object state(fields) and behavior(methods)
together. If you are creating class, you are doing encapsulation. It is also known as data
hiding.
It is to hide the implementation details from users. If a data member is private
it means it can only be accessed within the same class. No outside class can access
private data member (variable) of other class.
Department of Industrial and
Information Technology

Encapsulation
Advantages of encapsulation

• It improves maintainability, flexibility and re-usability


• The fields can be made read-only (If we don’t define setter methods in the class) or
write-only (If we don’t define the getter methods in the class).
• User would not be knowing what is going on behind the scene. They would only be
knowing that to update a field call set method and to read a field call get method but
what these set and get methods are doing is purely hidden from them.
Department of Industrial and
Information Technology

Encapsulation
Data Hiding
• Data hiding is referred to as encapsulation
• Distinguishes the outside interface to the class from its implementation
• Hides the implementation details of the class
• Forces the user to use an interface to access data
• Makes the code much more maintainable
Department of Industrial and
Information Technology

Encapsulation
How to implement encapsulation in java
1. Make the instance variables private so that they cannot be accessed directly from
outside the class. You can only set and get values of these variables through the
methods of the class.
2. Have getter and setter methods in the class to set and get the values of the fields.
3. The get method returns the variable value, and the set method sets the value.
4. 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:
Department of Industrial and
Information Technology

Access Keywords
Access keywords define the access to class members from the same
class and from other classes.

Access Modifiers are keywords used to specify the declared


accessibility of a member of a type.
Department of Industrial and
Information Technology

Access Keywords
The most common access keywords are:

Public: Allows access to the class member from any other class.
Private: Allows access to the class member only in the same class.
Protected: Allows access to the class member only within the same class and from inherited classes.
Internal: Allows access to the class member only in the same assembly.
Protected internal: Allows access to the class member only within the same class, from inherited
classes, and other classes in the same assembly.
Static: Indicates that the member can be called without first instantiating the class

Note. Class default access modifier = internal


Members of the class default modifier = private
Department of Industrial and
Information Technology

Access Keywords
The most common access keywords are:

Public: Allows access to the class member from any other class.
Private: Allows access to the class member only in the same class.
Protected: Allows access to the class member only within the same class and from inherited classes.
Internal: Allows access to the class member only in the same assembly.
Protected internal: Allows access to the class member only within the same class, from inherited
classes, and other classes in the same assembly.
Static: Indicates that the member can be called without first instantiating the class

Note. Class default access modifier = internal


Members of the class default modifier = private

You might also like