The document explains encapsulation in Java, a key concept of object-oriented programming that combines data and methods into a single unit, like a class. It emphasizes the importance of restricting direct access to an object's components to protect data from interference and misuse. An example code snippet demonstrates how to implement encapsulation using getter and setter methods in a Student class.
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 ratings0% found this document useful (0 votes)
2 views53 pages
JAVA UNIT 1
The document explains encapsulation in Java, a key concept of object-oriented programming that combines data and methods into a single unit, like a class. It emphasizes the importance of restricting direct access to an object's components to protect data from interference and misuse. An example code snippet demonstrates how to implement encapsulation using getter and setter methods in a Student class.
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/ 53
class Student {
private String name;
Encapsulation in Java; Encapsulation is one of the fundamental // Getter method concepts of object-oriented programming public String getName() { (OOP) in Java. It is the wrapping data return name; } (variables) and methods (functions) into a single unit like a class. It helps to restrict // Setter method direct access to some of the object's public void setName(String n) { name = n; components, which means protecting the } data from outside interference and misuse. }
public class Test {
public static void main(String[] args) { Student s = new Student(); s.setName("Rahul"); System.out.println("Student Name: " + s.getName()); } }