0% found this document useful (0 votes)
34 views2 pages

Encapsulation

Uploaded by

vanshvijay044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

Encapsulation

Uploaded by

vanshvijay044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Encapsulation - wrapping up of data and methods into a single unit is known as

encapsulation.
class stud
{
int rno;
char grade;
void input()
{
}
void disp()
{
}
}

Key aspects of encapsulation in Java:


Bundling of Data and Methods:
Encapsulation means grouping related data and the methods that manipulate that data
within a single class. This creates a self-contained unit that manages its own
state and behavior.
Data Hiding (Information Hiding):
An important aspect of encapsulation is data hiding. This is achieved by declaring
class fields (variables) as private, making them inaccessible directly from outside
the class.

Benefits:
Data Security and Integrity: By restricting direct access, encapsulation prevents
unauthorized or accidental modification of an object's internal state.

Modularity: Encapsulation promotes modularity by creating self-contained units,


making code easier to understand, maintain, and reuse.

Flexibility and Maintainability: Changes to the internal implementation of a class


can be made without affecting external code as long as the public interface
(getters and setters) remains consistent.

Validation and Control: Setter methods can incorporate validation logic to ensure
that data is set within acceptable parameters, further enhancing data integrity.

Access specifiers, also known as access modifiers in Java, are keywords that set
the accessibility or visibility of classes, methods, variables, and constructors.
They determine where a particular element can be accessed from within a Java
program, contributing to encapsulation and data hiding.
There are four primary access specifiers in Java:

Private:
Scope: Accessible only within the same class where it is declared.
Usage: Used for data members or methods that should not be directly accessed or
modified from outside the class, enforcing data hiding.

Default (Package-private):
Scope: Accessible only within the same package. No explicit keyword is used; it is
the default access level when no other modifier is specified.
Usage: Suitable for classes, methods, or variables that are intended to be used by
other classes within the same logical grouping (package) but not exposed to the
entire application.

Protected:
Scope: Accessible within the same package and by subclasses (even if in a different
package).
Usage: Often used for members that are intended to be inherited and potentially
overridden by subclasses, while still maintaining some level of protection from
unrelated classes.

Public:
Scope: Accessible from anywhere in the program, regardless of the class or package.
Usage: Employed for elements that need to be globally accessible, such as public
APIs, utility methods, or constants.
By using these access specifiers, developers can control the level of interaction
between different parts of a program, leading to more organized, maintainable, and
secure code.

Imprtant Note :-
In Java, the terms "friendly" and "default" refer to the same access level, which
is applied when no explicit access modifier (like public, private, or protected) is
specified for a class, method, or variable.

[Argument variable : - passed parameters (Actual param)]

You might also like