Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Encapsulation in Java

Encapsulation in Java is a core object-oriented programming principle that combines data and methods within a class, allowing for data hiding and controlled access through public methods. It is implemented by declaring class variables as private and using getter and setter methods to access and modify these variables, enhancing security and flexibility. Several examples illustrate how encapsulation works in practice, demonstrating the use of private variables and public methods to manage data access.

Uploaded by

mercy joyce
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Encapsulation in Java

Encapsulation in Java is a core object-oriented programming principle that combines data and methods within a class, allowing for data hiding and controlled access through public methods. It is implemented by declaring class variables as private and using getter and setter methods to access and modify these variables, enhancing security and flexibility. Several examples illustrate how encapsulation works in practice, demonstrating the use of private variables and public methods to manage data access.

Uploaded by

mercy joyce
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Encapsulation in Java



Encapsulation in Java is a fundamental OOP (object-oriented


programming) principle that combines data and methods in a class.
It allows implementation details to be hidden while exposing a
public interface for interaction.

Example: Below is a simple example of Encapsulation in Java.

// Java program demonstrating Encapsulation


class Programmer {
private String name;

// Getter and Setter for name


public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

public class Geeks {


public static void main(String[] args) {
Programmer p = new Programmer();
p.setName("Geek");
System.out.println("Name=> " + p.getName());
}
}

Output
Name=> Geek

Implementation of Java Encapsulation


In Java, encapsulation is implemented by declaring instance
variables as private, restricting direct access. Public getter methods
retrieve variable values, while setter methods modify them,
enabling controlled access. This approach allows the class to
enforce data validation and maintain a consistent internal state,
enhancing security and flexibility.
Encapsulation is defined as the wrapping up of data under a single
unit. It is the mechanism that binds together code and the data it
manipulates. Another way to think about encapsulation is, that it is a
protective shield that prevents the data from being accessed by the
code outside this shield.
 In encapsulation, the variables or data of a class are hidden from
any other class and can be accessed only through any member
function of its own class.
 A private class can hide its members or methods from the end
user, using abstraction to hide implementation details,
by combining data hiding and abstraction.
 Encapsulation can be achieved by Declaring all the variables in
the class as private and writing public methods in the class to set
and get the values of variables.
 It is more defined with the setter and getter method.

Example 1: Here is another example of encapsulation in Java


// Java program to demonstrate the Encapsulation.
class Person {
// Encapsulating the name and age
// only approachable and used using
// methods defined
private String name;
private int age;

public String getName() { return name; }

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

public int getAge() { return age; }

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


}

// Driver Class
public class Geeks {
// main function
public static void main(String[] args)
{
// person object created
Person p = new Person();
p.setName("John");
p.setAge(30);
// Using methods to get the values from the
// variables
System.out.println("Name: " + p.getName());
System.out.println("Age: " + p.getAge());
}
}

Output
Name: John
Age: 30
Example 2: In this example, we use abstraction to hide the
implementation and show the functionality.
class Area {
private int l; // this value stores length
private int b; // this value stores breadth

// constructor to initialize values


Area(int l, int b)
{
this.l = l;
this.b = b;
}

// method to calculate area


public void getArea()
{
int area = l * b;
System.out.println("Area: " + area);
}
}

public class Geeks {


public static void main(String[] args)
{
Area rect = new Area(2, 16);
rect.getArea();
}
}

Output
Area: 32
Example 3: The program to access variables of the class
Encapsulate Demo is shown below:
// Java program to demonstrate
// Java encapsulation

class Encapsulate {
// private variables declared
// these can only be accessed by
// public methods of class
private String geekName;
private int geekRoll;
private int geekAge;

// get method for age to access


// private variable geekAge
public int getAge() { return geekAge; }

// get method for name to access


// private variable geekName
public String getName() { return geekName; }

// get method for roll to access


// private variable geekRoll
public int getRoll() { return geekRoll; }

// set method for age to access


// private variable geekage
public void setAge(int newAge) { geekAge = newAge; }

// set method for name to access


// private variable geekName
public void setName(String newName)
{
geekName = newName;
}

// set method for roll to access


// private variable geekRoll
public void setRoll(int newRoll) { geekRoll = newRoll; }
}

public class TestEncapsulation {


public static void main(String[] args)
{
Encapsulate o = new Encapsulate();

// setting values of the variables


o.setName("Harsh");
o.setAge(19);
o.setRoll(51);

// Displaying values of the variables


System.out.println("Geek's name: " + o.getName());
System.out.println("Geek's age: " + o.getAge());
System.out.println("Geek's roll: " + o.getRoll());

// Direct access of geekRoll is not possible


// due to encapsulation
// System.out.println("Geek's roll: " +
// obj.geekName);
}
}

Output
Geek's name: Harsh
Geek's age: 19
Geek's roll: 51

You might also like