blog

Home / DeveloperSection / Blogs / Use of Access specifiers

Use of Access specifiers

Hemant Patel 1450 17-Jan-2017

Access specifiers:- Access specifier provide a facility to restricted another class to inherit the property of parents class. Its also provide a path to control the visibility of members like classes, variables and methods. Access specifiers is used for classes, constructor, variable, and method.

There are four types of access specifiers:-

1.public

2.private

3.protected

4.unspecified 

1.public:-In public access specifier we can assigned public specifier to different elements like classes, method, constructor and variable. 

class ClassName
{
Public:
          Int a;
}
int main()
{
ClassName obj;
Obj.a=10; //Allowed
}
 

2.private:-In private access specifiers we can not assign private specifier to any class. its only assign to variable ,method and constructor. In other words we can not create private class. 

Class ClassName
{
Protected:
Int a;
};
Int main()
{
ClassName obj;
Obj.a=20; // Not allowed, gives compiler error
}

 3.protected:-Protected access specifier is little bit same with private.But there is big difference between them. In protected specifiers, child class inherit the property of parent class with in same package. but in private class the method of private class is only access in same class and child class can not access the property of parent class.

Protected access specifiers can be also used for constructor, variable and method.

class ClassName
{
Private:
Int c;
};
Int main()
{
ClassName obj;
Obj.c=10;  //Not
Allowed,gives compiler error
}

 

4.Unspecified:-Its same as default. When we can not use any access specifiers,then its categorized as a unspecified. All classes, method, constructor and variable that’s can not be specified with any access specifiers is fall in this category.  

class ClassName
{
Int a;
};
int main()
 {
ClassName obj;
Obj.a=15;
          

Updated 17-Mar-2018
Exploring the online world is my passion. have experience of #content writing #SEO #Digital Marketing #On-Page #Lead Generation #Content Analyst #Marketing Analyst... I could never stop at one point, continuously wanted to acquire more and more skills. My work for any organization will be full of passion and hard work.

Leave Comment

Comments

Liked By