Do you know what is access specifier


As i always do let me again start with a good example. Access specifier is a level of permission you have to use someone’s belongings and the level of permission you give to someone.Think what it would be like if you try to drive someone’s private car parked in road.But you can so easily ride a car which belongs to your dad.So with this example you must have made something about the access specifier.

access specifier

access specifier


Access specifier are used to control the visibility of a variable or method.It defines when a method or variable is visible to another class.It’s mainly used for Encapsulation,which is concern with hiding the data in a class and making this class available only through the method.IT minimizes the accidental change of values. There are 4 types of access specifier: 1.pubic 2.protected 3.default 4.private lets see all of them in more detail.

public:

If the class and the things we find inside the class be it a data variables or the methods is accessible to everyone.”Think it as an air in atmosphere which belongs to everyone”. Use the public access specifier if you want to make the variable or methods or the class available to every class we will make.It’s not safe to use public for all the class because making it public means the outside class will have free access to our class and make some unwanted alteration.we don’t want our secure key to be pubic right. example:
public class NameDisplay
{
public String name;
public void display()
{
System.out.println(name);
}
}
class PublicDemo
{
public static void main(String[] args)
{
NameDisplay one=new NameDisplay();
one.name=”Tara”;
one.display();
}
}
Note: save the program as NameDisplay.java and compile it.
Run the PublicDemo as java PublicDemo Since the class NameDisplay is public so the class PublicDemo is able to access the NameDisplay class. also the NameDisplay has a public data variable name so the variable is being possible to be changed from the different class PublicDemo. Normally when we design a class make data variable as Private so that only the method inside the class can access it.Make the method as public.

protected:

“It’s just like a son using the car of his father.Father will use the car also allow anybody from his family to use it”. It means if a class has a protected methods and fields than those methods and fields are accessible only to that class and the class derive from that class,ie the subclasses.
example:
class NameDisplay
{ protected String name;
public void display()
{ System.out.println(name);
}
}
class PublicDemo extends NameDisplay
{
public static void main(String[] args)
{ NameDisplay one=new NameDisplay();
one.name=”Biswajit”;
one.display();
}
}
——-Description—— so the class NameDisplay has a data variable called name and its declared as protected means these variable can be used within the class NameDisplay and any class other classes that extend this class i.e inherits the NameDisplay class.Here we made the class PublicDemo and its a subclass of nameDisplay so that is why its able to access the name variable of the Namedisplay class which does not belong to it.

Default(no specifier)/Package:

when none of the access level is set,then it is of default type that’s why its named so.It is also known as package access specifier. In these access specifier the methods and fields will be accessible from inside the same package but not from the any where else. The default access specifier is useful when number of classes can be kept inside a same package and make the sharing among them more easily.
example:
class NameDisplay //default class
{ String name=”ankur”;
public void display()
{
System.out.println(name);
}
}
class PublicDemo
{
public static void main(String[] args)
{ NameDisplay one=new NameDisplay();
one.display();
}
}

Private:

“Its like your wife or husband who always belongs to you.You don’t really want to share it”. A private method and fields can be accessed within a same class to which the method and fields belong. The methods and fields are not accessible to other classes not even to the subclasses.It is the access specifier which has the high degree of data hiding and protection.
example:
private class PrivateDemo
{ private string name; public void setter(string name)
{
this.name=name;
}
public String getter()
{
return name;
}
}
// so i don’t think there is something you can do with this class since its private it cant be used in any other classes.Normally we don’t make a class private but we do make the class attribute as private. so my final words i hope it was helpful to you all .Please do comment and comment and try to correct me if i am missing somewhere thanks….

Advertisements

About tarasj

hi friends i to blog what i have learned .lets talk and share
This entry was posted in ..BASIC... Bookmark the permalink.