Access Specifiers
Data Abstraction
Encapsulation
Access Specifiers
It allows us to restrict the scope or visibility of a package, class, constructor,
methods, variables, or other data members.
By using access specifiers, we define how the members (attributes and
methods) of a class can be accessed.
The most common access specifiers are following.
Private
Public
Default
Protected
Access specifiers help implement:
• Encapsulation by hiding implementation-level details in a class
• Abstraction by exposing only the interface of the class to the
external world
The private access specifier is generally used to encapsulate or hide
the member data in the class
The public access specifier is used to expose the member functions as
interfaces to the outside world
Data Abstraction
• Abstraction denotes essential characteristics of an object that distinguish it
from all other kinds of objects.
• It is the process of hiding certain details and showing only essential
information to the user.
Faculty- Emp. id
Student- Reg. No
For a Doctor Patient
Name, Age, Old medical records
For a Teacher Student
Name, Roll Number/RegNo, Education background
The whole idea behind encapsulation is to hide the implementation details
from users.
If a data member is private it means it can only be accessed within the same
class. No outside class can access private data member (variable) of other
class.
Data can only be accessed by public methods thus making the private fields
and their implementation hidden for outside classes.
That’s why encapsulation is known as data hiding.
Why main() method is public, static and void in java ?
public : “public” is an access specifier which can be used outside the class. When main
method is declared public it means it can be used outside class.
static : To call a method we require object. Sometimes it may be required to call a method
without the help of object. Then we declare that method as static. JVM calls the main()
method without creating object by declaring keyword static.
void : void return type is used when a method doesn't return any value . main() method
doesn’t return any value, so main() is declared as void.