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 : Define the term Binary Binary : This is

    Binary: This is the number representation in base 2. In base 2, only digits 0 and 1 are utilized. Digit positions symbolize successive powers of 2.

  • Q : Define the term Graphical User Interface

    Graphical User Interface: A Graphical User Interface (abbreviated as GUI) is a part of a program which permits user interaction through graphical components, like menus, buttons, text areas, and so forth. Interaction frequently includes the usage of a

  • Q : What are Logical operators Logical

    Logical operators: The operators, like &&, ||, &, | and ^ which take two Boolean operands and generate a Boolean outcome. Employed as part of a Boolean expression, frequently in the condition of the control structure.

  • Q : Define Divide and conquer Divide and

    Divide and conquer: An approach to trouble solving which attempts to decrease an overall single big trouble into multiple simpler troubles.

  • Q : Explain Exclusive-or operator

    Exclusive-or operator: An exclusive-or operator (^) is both a Boolean operator and the bit manipulation operator. The Boolean version provides the value true when only one of its operands is true; or else it offers the value false. Likewise, the bit m

  • Q : Common Language Infrastructure or CLI

    What is the Common Language Infrastructure (CLI)? What relation does .NET have with the CLI?

  • Q : What does compatibility testing include

    What does compatibility testing include and who will perform the testing?

  • Q : Explain the relationship between XHTML

    Explain the relationship between XHTML and XML?

  • Q : What is Carriage return Carriage return

    Carriage return: The \r character. It is also used as a synonym for the `Return' or `Enter' key employed to terminate a line of text and the name derives from the carriage on mechanical typewriter.

  • Q : Define the term Instance Define the

    Define the term Instance: It is a synonym for object. The objects of a class are instantiated whenever a class constructor is invoked through the new operator.

©TutorsGlobe All rights reserved 2022-2023.