0% found this document useful (0 votes)
14 views10 pages

Chapter 6

Computer class 10

Uploaded by

veerthedarkruler
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)
14 views10 pages

Chapter 6

Computer class 10

Uploaded by

veerthedarkruler
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/ 10

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");
}
}

Here error is coming because protected members of class


defined in other package cannot be accessed without
inheritance.
Now after inheritance we can access protected members of
class defined in other package.
4. private specifier: - it is most restricted specifier among all
specifiers. In java, class and the interface cannot be private,
but members of a class can be private which can be accessed
only within the class.
package pack1;
public class specifier
{
private void show()
{
System.out.println("private method of class specifiers");
}
}

Private specifier can be accessed only within a class; it cannot


be accessed outside the class that is why the error occurred.
For example:-
package pack1;
public class specifier
{
private void show()
{
System.out.println("private method of class specifier");
}
public static void main(String args[])
{
specifier ss=new specifier();
ss.show();
}
}
Output:- private method of class specifier.

Specifier Class Subclass Package outside


Private Yes
Protected Yes Yes Yes
Public Yes Yes Yes Yes
Default Yes Yes Yes

Scope and visibility rules of variables


Scope of a variable defines the section of source code from
where the variable can be accessed. Variables declared inside
a scope are not visible to the code that is defined outside the
scope.
Lifetime of a variable describes how long the variable
continues to exist before it is destroyed.

You might also like