Q. Explain the use of private, public, protected access specifies.
OR
Explain the effect of using the access specifies with class members on inheritance.
Ans. One of the methods in object oriented programming is encapsulation. It consists the hiding of data in a class and making the class availability only through methods. It is the way the chance of making this class available only making accidents mistakes in changing values is minimized. Java allows you to control access to classes, methods, and fields via so called access specifies. Java offers four accesses specifies, listed below in decreasing accessibility:
. Public
. Protected
. Default (no specifies)
. Private
We look at these access specifies in more detail.
Public: Public methods, classes and fields can be available from everywhere. The one and only constraint is that a file with java source code can only hold one public class whose name must also go with the filename. If it exists, this public class represents the application or the applet, in which the public class represents the application the public keyword, is necessary to enable your web browser or applet viewer to show the applet. You use public classes, methods, or fields only if to offer access to these entities and if this access cannot do any harm. An example of a square determined by the position of its upper of its upper left corner and its size.
Public class square
{ // public class
Public x, y, size; // public instance variables
}
Protected: Protected techniques and fields can simply be accessed within the same class to which the methods and fields belongs, in its subclasses, and in classes of the similar package, but not from somewhere else. You use the protected access level when it is appropriate for a class subclasses to have access to the method or field, but nor for the unrelated classes.
Default (no specifies): If you can't set access to precise level, then such a method, class, or field will be accessible from inside the same package to which the class method or field belongs but not from exterior to this package. This access level is suitable if you are developing packages. For example a geometry package that contains square and tilling classes may be easier and cleaner to implement if the coordinates of the upper left corner of a square are directly available to the tilling class but not outside the geometry package.
PRIVATE: private methods and fields may only be accessed in the similar class to which the methods and fields belong. Private methods and fields are not visible within the subclasses and are not inherited by subclasses. Thus the private access specifier is contradictory to the public access specifier. It is mostly used for encapsulation data are hidden within the class and accessory methods are provided. An example in which the position of the upper left corner of a square can be set or obtained by accessory methods but individual coordinates are not accessible to the user.
Public class Square {// public class
Private double x, y // private (encapsulated) instance variables
Public set corner (Int x, Int y) { // setting values of private fields
This.x=x;
This.y=y;
}
Public get corner () { // setting values of private fields
Return point(x, y);
}
}
Hence the difference between the default access which is in fact more restricted than the protected access. Without access specifier (the default choice) techniques and variables are accessible only within the class that defines them and within classes that are part of the same package. They are not observable to subclasses except these are in the similar package. Protected methods and variables are noticeable to subclasses in spite of in which package they are in.