You are on page 1of 13

Object Oriented Programming

Section (3)

Eng. Yasmine Mahmoud


Assistant Lecturer
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.
Encapsulation
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.


Get and Set
You learned from the previous sections 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:
Get and Set
public class Person
The get method returns the value of the
{ variable name.
// private = restricted access
private String name;

// Getter public String getName()


{ The set method takes a parameter (newName)
return name; and assigns it to the name variable.
}
// Setter
public void setName(String newName)
{
this.name = newName; The this keyword is used to refer to the
} current object.
}
When accessing variable is declared as private
As the name variable is declared as private, we cannot access it from outside this class:

public class MyClass


{
public static void main(String[] args)
{
Person myObj = new Person();
myObj.name = "John"; // error
System.out.println(myObj.name); // error
}
}

If the variable was declared as public -> output is John


getName() & setName()
Instead, we use the getName() and setName() methods to access and update the
variable:

public class MyClass


{
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"
• * Complete this class to do the following:
* Implement getter and setter for the age attribute
* Age should be within the range of 20 to 40 inclusive
* Implement a private method to check name as follows:
• * Name should not be empty
* Name should include only English characters
* Length of Name should not exceed 20 characters.
• * Can we implement one method to get person data
* Can we implement one method to set person data
* Add phone number and email to the Person class
* Add birth date that contains Day, Month, and Year
* Validate phone number ( should be 11 digits starting with 010, 012, or 015) * Validate
email (should be in the pattern username@subdomain.domain)
• * Implement birth date as a class (Do not use Java Date class)
* Implement the required methods to set and get day, month, and year * Implement the
required methods to validate day, month and year

You might also like