0% found this document useful (0 votes)
4 views5 pages

Notes12 (Encapsulation)

Uploaded by

V.Annapeachi
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
4 views5 pages

Notes12 (Encapsulation)

Uploaded by

V.Annapeachi
Copyright
© © All Rights Reserved
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/ 5

Encapsulation:

• Encapsulation is one of the OOPs principles.


• Binding the data member and related functions together to act as a
single unit inside the classbody is known as Encapsulation.

• encapsulation specifies that the data must be protected by using appropriate


access specifier.
• The private access specifier is used to achieve Encapsulation.
• A private data member is also known as an Encapsulated member.
• To access the encapsulated member of a class getters and setters are used.
• Getters are used to access the encapsulated members of a class. It is also known as
accessor.
• Setters are used to manipulate the encapsulated member of a class. It is also
known as a mutator.
• Class is an encapsulation of data and functions.
• A package is an encapsulation of classes
• Jar (Java Archived file) is an encapsulation of packages
• Java by default supports encapsulation without encapsulation principle, we cannot
define any program in java.

Java Bean Class:


A Java Bean Class is a class which follows the below concepts
1) The class must have public access specifier
2) The data members must be declared as private
3) The constructor should be public
4) The class must provide public getter and setter methods.

Java bean class is a very good example of encapsulation

pg. 1
Advantage of encapsulation:
➢ Data Hiding:
The process of restricting direct access to the attributes of the object and providing
controlled and restricted access to the properties of the object is called data hiding
Through data hiding we can achieve
1) Security
2) Validation

Programs:
1) public class Product {

private double price;

public double getPrice() {


return price;
}

public void setPrice(double price) {


//validation

if(price > 0)
{
this.price = price;
}
else {
System.out.println("enter the price greater than 0");
}
}
}
public class ProductDriver {

public static void main(String[] args) {

Product p1 = new Product();


p1.setPrice(-2000);

System.out.println(p1.getPrice());
}
}

Output:
enter the price greater than 0
0.0

pg. 2
2)
public class Student {

private int age;


private double perc;

public int getAge() {


return age;
}

public void setAge(int age)


{
if(age>15 && age<=50)
{
this.age = age;
}
else {
System.out.println("age criteria is not matching");
}

public double getPerc()


{
return perc;
}

public void setPerc(double perc)


{
if(perc >= 35 && perc <=100)
{
this.perc = perc;
}
else {
System.out.println("perc should be >=35");
}
}

pg. 3
public class Test {

public static void main(String[] args) {

Student s1 = new Student();

s1.setAge(21);
s1.setPerc(60.12);

System.out.println(s1.getAge());
System.out.println(s1.getPerc());

System.out.println("=======================");

Student s2 = new Student();

s2.setAge(16);
s2.setPerc(30.12);

System.out.println(s2.getAge());
System.out.println(s2.getPerc());

Output:
21
60.12
=======================
perc should be >=35
16
0.0

pg. 4
pg. 5

You might also like