0% found this document useful (0 votes)
4 views

Java Encapsulation

Uploaded by

pherjanes0215
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Encapsulation

Uploaded by

pherjanes0215
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Java

Encapsulation
The meaning of Encapsulation, is to make
sure that "sensitive" data is hidden from
users. To achieve this, you must:

•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
You learned from the previous chapter that 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
Example explained
public class Person { The get method returns
private String name; // private =restricted
the value of the
access
variable name.
// Getter The set method takes a
public String getName() { parameter (newName) and
return name; assigns it to
} the name variable.
The this keyword is used
// Setter to refer to the current
public void setName(String newName) { object.
this.name = newName; However, as the name variable is
} declared as private,
we cannot access it from outside
} this class:
Example
public class Main {
public static void main(String[] args) {
Person myObj = new Person();
However, as
myObj.name = we try to access a private variable, we get an error:
"John";
System.out.println(myObj.name);
} MyClass.java:4: error: name has private access in Person
myObj.name = "John";
} ^
MyClass.java:5: error: name has private access in Person
System.out.println(myObj.name);
^
2 errors
If the variable was declared
as public, we would expect the
following output: John
Instead, we use the getName() and setName() methods to access and
update the variable:

Example

public class Main {


public static void main(String[] args) {
Person myObj = new Person();
myObj.setName("John"); // Set the value of the name
variable to "John"
System.out.println(myObj.getName());
}
}
// Outputs "John"
Why Encapsulation?
 Better control of class attributes and methods
 Class attributes can be made read-only (if
you only use the get method), or write-
only (if you only use the set method)
 Flexible: the programmer can change one
part of the code without affecting other parts
 Increased security of data
EXERCISE:
Fill in the Blanks: Getters and Setters in Java
Consider a simple class Person that represents a person with a name and an age. Your task is to complete the class by filling in the blanks for the getter and
setter methods.
public class Person {
// Private fields for encapsulation
private String name;
private int age;

// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}

// Getter for name


public ______________ getName() {
return ______________;
}

// Setter for name


public ______________ setName(______________ name) {
this.______________ = name;
}

// Getter for age


public ______________ getAge() {
return ______________;
}

// Setter for age


public ______________ setAge(______________ age) {
this.______________ = age;
}
}
public class Person { public class Person {
// Private fields for encapsulation // Private fields for encapsulation
private String name; private String name;
private int age; private int age;

// Constructor // Constructor
public Person(String name, int age) { public Person(String name, int age) {
this.name = name; this.name = name;
this.age = age; this.age = age;
} }

// Getter for name // Getter for name


public ______________ getName() { public String getName() {
return ______________; return name;
} }

// Setter for name // Setter for name


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

// Getter for age // Getter for age


public ______________ getAge() { public int getAge() {
return ______________; return age;
} }

// Setter for age // Setter for age


public ______________ setAge(______________ age) { public void setAge(int age) {
this.______________ = age; this.age = age;
} }
} }
Complete the Getter and Setter Methods in Java
Fill in the blanks to complete the setter method for the name field.
public class Person {
private String name;

// Getter method
public String getName() {
return name;
}

// Setter method
public void __________(String name) { 1.
this.__________ = __________; 2.
}
}
Complete the getter method for the age field:

public class Person {


private int age;

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

// Getter method
public __________ getAge() { 3.
__________ __________; 4.
}
}
Fill in the blanks to complete both getter and setter
methods for the salary field.

public class Employee {


private double salary;

// Getter method
public __________ getSalary() { 5.
return __________; 6.
}

// Setter method
public void __________(double salary) { 7.
this.__________ = __________; 8.
}
}

You might also like