--%>

Use of private, public, protected access specifies

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.

   Related Questions in Programming Languages

  • Q : What is an Implicit type conversion

    Implicit type conversion: The type conversion which does not need a cast. Implicit type conversions usually do not comprise any loss of information. For example, joining an integer operand with a floating point operand in an arithmetic expression will

  • Q : Define Uniform Resource Locator Uniform

    Uniform Resource Locator: It is a Uniform Resource Locator (abbreviated as URL) expands the concept of file access from a wholly local context to one in which the resources are named uniformly, irrespective of where they may be physically situated. A

  • Q : What is u-area Explain what is meant by

    Explain what is meant by the term u-area (user area)?

  • Q : Explain Method overloading Method

    Method overloading: Two or more techniques with similar name stated within a class are said to be overloaded. This exerts to both constructors and other methods. The overloading applies via a class hierarchy, thus a sub class may overload a method sta

  • Q : Libraries involved in Windows

    Explain the important libraries involved in Windows programming?

  • Q : What is Static method Static method :

    Static method: The static method (also termed as a class method) is one with static reserved word in its header. The static methods vary from all other methods in that they are not related with any specific instance of the class to which they fit in.

  • Q : Programming with C# QUESTION 1      

    QUESTION 1       The following UML diagram describes an abstract class Customer. This class is to be used as part of a Company's inventory system. The inventory system will contain many different types of customers.  A separate s

  • Q : Write a recursive implementation of

    Assignment 5 Selecting Array Elements Implement the following C++ code in assembly language, using the block-structured .IF and .WHILE directives. Assume that all variables are 32-bit signed integers: int array[] = {10,60,20,33,72,89,45,65,72,18}; int sample = 50; intArraySize = s

  • Q : What are the major issues related with

    What are the major issues related with multiprocessor caches and how might you resolve them?

  • Q : Walk through the steps essential to

    How can you walk through the steps essential to parse XML documents?