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 Multiprogramming system

    Define the term Multiprogramming system: It is an operating system which is able to run multiple programs parallel.

  • 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 : What is an Anonymous class Anonymous

    Anonymous class: It is a class formed without a class name. Such a class will be a sub class or an implementation of an interface, and is generally formed as an actual argument or returned as a method outcome. For example:

    Q : Explain the way of the Orbeon Explain

    Explain the way of the Orbeon.

  • Q : Define Radio buttons Radio buttons : It

    Radio buttons: It is a group of selectable components in which merely one component might be selected. The selection of one of the group that causes the previously chosen component to be deselected.

  • Q : Define the term Boot Boot: Whenever a

    Boot: Whenever a computer is switched on it is said to be `boot up'. This word comes from the phrase, “Pulling yourself up by your bootstraps”. Before a computer is ready to be utilized, it should load the programs which it requires from i

  • Q : Explain the relationship between XHTML

    Explain the relationship between XHTML and XML?

  • Q : Advantage of wrapping database calls in

    What is the advantage of wrapping database calls in MTS transactions?

  • Q : What is Low level programming languages

    Low level programming languages: It is frequently termed as `assembly languages’; these give little more than the fundamental instruction set of a particular Central Processing Unit. Therefore programs written in low level programming languages

  • Q : Explain Default initial value Default

    Default initial value: It is the default value of any variable not explicitly initialized whenever it is declared. The fields of numeric primitive types contain the value zero by default, Boolean variables encompass the value false, char variables enc

©TutorsGlobe All rights reserved 2022-2023.