0% found this document useful (0 votes)
26 views20 pages

ENCAPSULATION

Encapsulation is a core concept in object-oriented programming that combines data and methods into a single unit, enhancing modularity, maintainability, and security. It allows for data hiding, abstraction, and promotes a modular design, which simplifies code maintenance and protects data integrity. The document also provides examples of implementing encapsulation in Java through private members, validation, and accessor methods.

Uploaded by

adinaabid99
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)
26 views20 pages

ENCAPSULATION

Encapsulation is a core concept in object-oriented programming that combines data and methods into a single unit, enhancing modularity, maintainability, and security. It allows for data hiding, abstraction, and promotes a modular design, which simplifies code maintenance and protects data integrity. The document also provides examples of implementing encapsulation in Java through private members, validation, and accessor methods.

Uploaded by

adinaabid99
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/ 20

ENCAPSULATION

SAJEEL_UR_REHMAN DSAI-242102023
REHAN ISHFAQ DSAI-242102015
ASAD_UR_REHMAN DSAI-242102011
WHAT IS ENCAPSULATION ?

Encapsulation is a fundamental principle in object-oriented


programming that binds data and methods into a single unit,
providing a level of abstraction and information hiding that
enhances code modularity, maintainability, and security.
IMPORTANCE OF ENCAPSULATION IN JAVA

Modular Design
Encapsulation enables the creation of self-contained,
reusable component that can be easily integrated into
1 larger system.
Code Maintainability
Encapsulation makes it easier to modify the internal
implementation of an object without effecting the external
2 code that uses it.

Data Security
Encapsulation protects the internal state of an object,
3 ensuring data integrity and preventing unintended
modifications
PRINCIPAL OF ENCAPSULATION
DATA HIDING ABSTRACTION Modularity
• Encapsulation • It provides a • Encapsulation
hides the layer of promotes
internal abstraction, modular
implementatio allowing design, making
n details of an developers to it easier to
object, interact with maintain, test,
exposing only objects without and extend the
the necessary needing to codebase.
methods and understand
properties to their complex
BENEFITS OF ENCAPSULATION
Data Integrity Flexibility Maintainability
• Encapsulation • Changes to the • Encapsulation
ensures that an internal promotes code
object's internal implementation reuse, reduces
data is accessed of an object can debugging
and modified be made without efforts, and
only through the affecting the makes the
object's own code that uses it, codebase more
methods, as long as the readable and
maintaining data public interface manageable.
consistency and remains the
preventing same.
IMPLEMENTING ENCAPSULATION IN CODE

• Declare Private Members


Use private access modifiers to hide the
EXAMPLE:-
internal data and implementation details
of an object. Private int a;
• Enforce Validation
Public setA(int x){
Validate input data and enforce business
rules within the object's methods to
maintain data integrity.
a=x; }
• Provide Accessor Methods
Public getA(){
Implement public getter and setter
methods to control access to the object's Return a; }
private members.
PROGRAM:-
public class A{ OUTPUT
“ 100 ”
private int value;
public void setValue(int x){
value=x; }
public int getValue(){
return a; }
public static void main(String[ ] args){
A r=new A();
r.setValue(100);
System.out.print( r.getValue() );
}
Access Modifiers and Encapsulation and
Information Hiding Data Abstraction
• Private • Encapsulation
Members marked as private Encapsulation combines data
can only be accessed within and methods into a single
the same class, providing unit, hiding the internal
the highest level of implementation details.
information hiding. • Data Abstraction
• Protected Abstraction simplifies
Protected members are complex systems by
accessible within the same providing a high-level
class and its subclasses, interface and hiding the
allowing controlled access underlying complexity.
for inheritance. • Synergy
• Public Encapsulation and
Public members can be abstraction work together to
accessed from anywhere, create modular,
CODE EXAMPLE
class Area{
private int x;
public void setX(int a){
x=a;
}
public int getX(){
return x*x;
}
}
CODE EXAMPLE:
public class Square {
public static void main(String[] args){
Area obj = new Area();
obj.setX(50);
System.out.print(obj.getX());
}
}
CODE EXAMPLE
class Detail{

private int age;

private float height;

public void setAge(int age){

this.age=age;

public void setHeight(double height){

this.height=(float)height;

public int getAge(){

return age;

}
CODE:
public float getHeight(){
return height;
}
}
public class Person {
public static void main(String[] args) {
Detail obj = new Detail();
obj.setAge(23);
obj.setHeight(4.12);
System.out.println("person's age is: "+obj.getAge());
System.out.println("person's height is:"+obj.getHeight());
}
}
OUTPUT:
ANY
QUESTIO
N
THANK
YOU

You might also like