What is class ? How does it accomplish data hiding ? Explain with an example?
Answers
Class
A class is a collection of objects which share certain characteristics and behaviour . A class is a composite datatype since it uses several datatypes within it .
A class hides data by restricting to the curly braces in which it is declared
Here is an example :
class sum
{
void main()
{
int a=4;
int b=6;
int c=a+b;
System.out.println("Sum="+c);
}
}
Now lets see another class :
class add
{
void main()
{
System.out.println(c);
System.out.println("Loser");
}
}
Now this class if written just after the first one will show an error.
Cannot find variable c
It is because the previous class hides its objects ( variable c ) by data hiding.
This way a class limits the access of data securing it!
Hope it helps
_____________________________________________________________________