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 : Function of TCHAR used in Windows

    Write down a program to show the function of TCHAR used in Windows Programming?

  • Q : Who Issues Certificates and how Who

    Who Issues Certificates and How?

  • Q : Explain Cohesion Cohesion : The level

    Cohesion: The level to which a component executes a single well-stated task. A strongly cohesive technique, for example, will execute a single task, like adding an item to a data structure, or sorting several data, while a weakly cohesive technique wi

  • Q : Are you sure that XHTML element name

    Are you sure that XHTML element name case sensitive? Answer: XHTML element names surely are case sensitive. Every element names should be written within lower case l

  • Q : Define Binary search Binary search :

    Binary search: This is a search of sorted data, in which the middle place is examined first. The search continues with either the right or the left part of the data, therefore removing half the remaining search space. This procedure is repeated at eac

  • Q : Define Static nested class Static

    Static nested class: It is a nested class with static reserved word in its header. Dissimilar inner classes, objects of static nested classes contain no enclosing object. They are as well termed as nested top-level classes.

  • Q : Define Thread Thread : It is a

    Thread: It is a lightweight procedure which is managed by the Java Virtual Machine (abbreviated as JVM). Support for threads is given by the Thread class in java.lang package.

  • Q : Define the term Icon Icon : It is an

    Icon: It is an image intended to communicate the language-or culturally-independent meaning.

  • Q : Tower of Hanoi Puzzle program using C#

    here I attached the assignment and examples of how can you do this assignments. I will post others in the next week as well.

  • Q : Define difference between write through

    Define the difference between write through and write back cache?

©TutorsGlobe All rights reserved 2022-2023.