Chapter 6
Chapter 6
Encapsulation
Wrapping up of data and methods into a single unit known as
class. It promotes physical hiding. The concept is similar to a
pencil box where a student keeps his pencil, pen, eraser and
sharpener in a single box and the items inside the box are not
visible outside.
Encapsulation=code {} + data
It can be achieved by declaring all the variables as private in a
class and methods as public.
EXAMPLE:-
class mains
{
public static void main(String args[])
{
encapsulation e1=new encapsulation();
e1.a=20;//error
e1.b=30;//error
e1.getdata(10,20);
e1.display();
}
}
class encapsulation
{
private int a,b;//data hiding
public void getdata(int x,int y)
{
a=x;
b=y;
}
public void display()
{
System.out.println(a+"\t"+b);
}
}
//you all can see a & b is not accessible as they r private
//after removing private keyword it is accessible .
Access Specifiers
Access specifiers specify or restrict the access of a class,
constructor, data members, methods and variables.
Types of specifiers
There are four types of access specifiers:
1. private
2. public
3. protected
4. default
Access specifiers serve the important purpose of defining the
boundary between the accessible and inaccessible parts of a
class. This feature is a way of implementing encapsulation in
Java. The protected access specifier is specific to the concept
of inheritance.
1. default specifier :- If there is no specifier specified for a
class, variable, method or data member then a specifier is
automatically specified by default, called default specifier. It
can be accessed only within a package, cannot be accessed
from outside package.
Example
package pack1;
class first
{
void display()
{
System.out.println(“class first inside package pack1”);
}
}
Now if i want to access this class in the same package, then it
can be accessed but in other package it will access only when
it becomes public.
package pack1;
class second
{
public static void main(String args[])
{
first ff=new first();
ff.display();
}
}
Output:- class first inside package pack1.
package pack2;
import pack1.first;
class run
{
public static void main(String args[])
{
first ff=new first();
ff.display();
}
}
When we run this program one error will come
“pack1.first is not public in pack1; cannot be accessed from
outside package”
2. public specifier: - public specifier allows access from
anywhere in the program within a package or from outside
package. This specifier has widest scope of accessibility and
visibility.
package pack1;//first statement.
public class first//public specifier
{
public void display()
{
System.out.println("hello display");
}
}
package pack2;
import pack1.first;
class tt
{
public static void main(String args[])
{
first f=new first();
f.display();
}
}
3. protected specifier: - it is more restricted than public
specifier. A class cannot be protected but member of a class
can be protected which can be accessed by other classes in
same package and by a subclass of another package by using
inheritance.
package pack1;
public class specifier
{
protected void show()
{
System.out.println("show method of protected
specifier");
}
}