Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Encapsulation in Java

Rating
-
Sold
-
Pages
3
Uploaded on
19-05-2024
Written in
2023/2024

The document can be contain the deep explanation of the java programming and it can be provide the program about the encapsulation and it can be contain the methods of abstraction

Institution
Course

Content preview

Encapsulation in Java
Encapsulation is a fundamental concept in Java that refers to the practice of
wrapping data (variables) and methods that act on that data within a single unit, a
class. Encapsulation is used to hide the internal implementation details of a class
and prevent unauthorized access to the data.

Protecting Class Properties and Methods
In Java, encapsulation is achieved using access modifiers such as private,
protected, and public. These access modifiers determine the accessibility of class
properties and methods.

private: Only accessible within the same class
protected: Accessible within the same class, package, and subclass
public: Accessible from anywhere
Student Class Encapsulation
Here's an example of encapsulation in a Student class:

public class Student {

// private variables
private String name;
private int age;
private double gpa;

// constructor
public Student(String name, int age, double gpa) {
this.name = name;
this.age = age;
this.gpa = gpa;
}

// getter methods
public String getName() {
return name;
}

public int getAge() {
return age;
}

public double getGPA() {
return gpa;
}

// setter methods
public void setName(String name) {
this.name = name;
}

public void setAge(int age) {
this.age = age;
}

public void setGPA(double gpa) {
this.gpa = gpa;
}
}
In this example, the name, age, and gpa variables are declared as private to
prevent unauthorized access. Getter and setter methods are provided to access and

Written for

Course

Document information

Uploaded on
May 19, 2024
Number of pages
3
Written in
2023/2024
Type
Class notes
Professor(s)
Lakchaya
Contains
All classes

Subjects

$10.29
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
lakchayalaks

Get to know the seller

Seller avatar
lakchayalaks Bharathi Educational Intitutions
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
13
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions