0% found this document useful (0 votes)
63 views22 pages

Encapsulation

The document provides an overview of Object Oriented Programming concepts in Java, focusing on encapsulation and access modifiers. It explains how encapsulation protects data by making class members private and using setter and getter methods for access. Additionally, it details the four types of access modifiers: private, default, protected, and public, along with their accessibility and examples.

Uploaded by

parirani1535
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)
63 views22 pages

Encapsulation

The document provides an overview of Object Oriented Programming concepts in Java, focusing on encapsulation and access modifiers. It explains how encapsulation protects data by making class members private and using setter and getter methods for access. Additionally, it details the four types of access modifiers: private, default, protected, and public, along with their accessibility and examples.

Uploaded by

parirani1535
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/ 22

SIBA Computer Science Department

Object Oriented Programming using


Java

Presented By:
Kamil Shaheen
Agenda

Recap of Last Lecture


Intro to Encapsulation
Access modifiers
Setter getter methods
Examples:
❑ Access private data through constructor

❑ Access private data through methods


Recap

What is a class and how to create it?


What is object and how to create it?
Static variables and methods
Constructors and its types
Constructor Overloading
this keyword
Encapsulation

It is a protective shield that prevents the data from


being accessed by the code outside this shield.
Encapsulation in Java is a process of wrapping code
and data together into a single unit, for example, a
capsule which is mixed of several medicines.
We can create a fully encapsulated class in Java by
making all the data members of the class private. Now
we can use setter and getter methods to set and get the
data in it.
It is achieved by making the members or methods of a class
private
Encapsulation can be achieved by Declaring all the variables in
the class as private and writing public methods(setter and getter)
in the class to set and get the values of variables.
Advantages:
● It improves maintainability, flexibility and re-usability
● The fields can be made read-only (If we don’t define setter
methods in the class) or write-only (If we don’t define the
getter methods in the class)
● User would not be knowing what is going on behind the
scene. They would only be knowing that to update a field call
set method and to read a field call get method but what these
set and get methods are doing is purely hidden from them.
● Encapsulation helps us to keep related fields and methods
together, which makes our code cleaner and easy to read
Access Modifiers

The access modifiers in Java specifies the accessibility


or scope of a field, method, constructor, or class. We can
change the access level of fields, constructors, methods,
and class by applying the access modifier on it.
There are four types of Java access modifiers:
● Private

● Default

● Protected

● Public
Private

The access level of a private modifier is only within the


class. It cannot be accessed from outside the class.
Output?
What will if we write private with
constructors?
If you make any class constructor private, you cannot
create the instance of that class from outside the class.

Note:A class cannot be private or protected except nested


class.
Default

If we do not explicitly specify any access modifier for


classes, methods, variables, etc, then by default the
default access modifier is considered.
The access level of a default modifier is only within the
package. It cannot be accessed from outside the
package
It provides more accessibility than private. But, it is
more restrictive than protected, and public.
Example

package pack;
class A{
void msg(){System.out.println("Hello");}
}
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A(); //Compile Time Error
obj.msg(); //Compile Time Error
}
}
Protected

The access level of a protected modifier is within the


package and outside the package through child class. If
you do not make the child class, it cannot be accessed
from outside the package.
The protected access modifier can be applied on the
data member, method and constructor. It can't be
applied on the class.
Example:
package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}

package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
Hello
}
Public

The access level of a public modifier is everywhere. It


can be accessed from within the class, outside the class,
within the package and outside the package.
It has the widest scope among all other modifiers.
Example
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}

package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
} Hello
}
Table : Access Modifiers
Encapsulation ….

As encapsulation mean hide data from sensitive user,


so to achieve this we declare class variables/attributes
private.
If we want to access the value of privates variables so
how can we do it?
● Setter/getter methods
● Constructors
Setter and getter methods

As we know that private variables are only accessible


within same class. So if we want to access them outside
the class then we have the concept of setter getter
methods.
● Getter method used to get/return the value of
private variables
● Setter method used to set the value of private
variables
Example
❑ Make member variables private
❑ Access them through public member method
Accessing private member variables through setter getter
member methods
Accessing private member variables through constructor

You might also like