0% found this document useful (0 votes)
23 views1 page

JAVA Encapsulation

The document discusses encapsulation in object-oriented programming. It explains that private variables can only be accessed within a class, but public get and set methods can provide access to private variables from outside the class. An example shows a class with a private name variable and public getName() and setName() methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views1 page

JAVA Encapsulation

The document discusses encapsulation in object-oriented programming. It explains that private variables can only be accessed within a class, but public get and set methods can provide access to private variables from outside the class. An example shows a class with a private name variable and public getName() and setName() methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Encapsulation

 declare class variables/attributes as private


 provide public get and set methods to access and update the value
of a private variable

Get and Set

private variables can only be accessed within the same class (an outside
class has no access to it). However, it is possible to access them if we
provide public get and set methods.

The get method returns the variable value, and the set method sets the


value.

Syntax for both is that they start with either get or set, followed by the name
of the variable, with the first letter in upper case:

Example
public class Person {

private String name; // private = restricted access

// Getter

public String getName() {

return name;

// Setter

public void setName(String newName) {

this.name = newName;

You might also like