top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java Access Specifiers

0 votes
600 views

Java Access specifiers

Java comes with four access specifiers namely, public, private, protected, and default. The details are as follows:

  • Public

The public access specifier is the least restrictive of all access specifiers. A field, method, or class declared public is visible to any class in a java application in the same package or in another package.

  • Private

The private access specifier is the most restrictive of all access specifiers. The private access specifier cannot be used for classes and interfaces as well as fields and methods of an interface. Fields and methods declared private cannot be accessed from outside the enclosing class. A standard convention is to declare all fields private and provide public accessor methods to access them. Thus when data is important, sensitive, and con not be shared with others, it is declared as private.

  • Protected

The protected access specifier is used with classes that share a parent-child relationship which is referred to as inheritance. The protected keyword cannot be used for classes and interfaces as well as fields and methods of an interface. Fields and methods declared protected in a parent or super class can be accessed only by its child or subclass in another packages. However, classes in the same package can also access protected fields and methods, even if they are not a subclass of the protected member’s class.

  • Default

The default access specifier is used when no access specifier is present. The default specifier gets applied to any class, field, or method for which no access specifier has been mentioned. With default specifier, the class, field, or method is accessible only to the classes of the same package. The default specifier is not used for fields and methods within an interface.

                                        Figure: Using Access Specifiers

Class A with four data members having public protected, default, and private access specifiers. Class B is a child class of A and class C is another class belonging to the same package, Package1. From the figure it is clear that they belong to the same package.

The package, package2 consists of classes D and E. Class D can only access the public members of class A. However, class E can access public as well as protected members of class A even though it belongs to another package. This is because class E is a child class of A. However, none of the classes B,C,D, or E can access the private members of class A.

Access Specifier

Class

Package

Subclass

World

public

Y

Y

Y

Y

Protected

Y

Y

Y

N

No modifier (default)

Y

Y

N

N

Private

Y

N

N

N

                                                   Table: Access Levels of Access Specifiers

The first column states whether the class itself has access to its own data members. As can be seen, class can always access its own members. The second column states whether classes within the same package as the owner class (irrespective of their parentage) can access the member. As can be seen, members can be accessed except private members.

The third column states whether the subclasses of a class declared outside this package can access a member. In such cases, public and protected members can be accessed. The fourth column states whether all classes can access a data member.

posted Jul 13, 2017 by Ayush Srivastav

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Format Specifiers

Whenever an output is to be displayed on the screen, it needs to be formatted. The formatting can be done with the help of format specifiers in java. The printf() method introduced in J2SE 5.0 can be used to format the numerical output to the console.

 

The table lists some of the format specifiers in java.

Format Specifier

Description

%d

Result formatted as a decimal integer

%f

Result formatted as a real number

%o

Result formatted as an octal number

%e

Result formatted as a decimal number in scientific notation

%n

Result is displayed in a new line

 

 

 

The code snippet demonstrates the use of various format specifiers.

int i = 55/22;

//decimal integer

System.out.printf (“55/22 = %d %n”,i);

// pad with zeros

double q = 1.0/2.0;

System.out.printf (“1.0/2.0 = %09.3f %n”, q);

//Scientific notation

q = 5000.0 / 3.0;

System.out.printf (“-10.0/0.0= %7.2e %n”, q);

Scanner Class

 The scanner class allows the user to read values of various types. To use the scanner class, pass the InputStream object to the constructor as shown below.

Scanner input = new scanner (System.in);

Here, input is an object of scanner class and, System.in is an input stream object. The table shows the different methods of the scanner class that can be used to accept numerical values from the user.

Method

Description

nextByte()

Returns the token as a byte value

nextInt()

Returns the next token as an int value

nextLong()

Returns the next token as a long value

nextFloat()

Returns the next token as a float value

nextDouble()

Returns the next token as a double value

 

 

 

Escape Sequences           

An escape sequence is a special sequence of characters that is used to represent characters, which cannot be entered directly into a string. For example, to include tab spaces or a new line character in a line or to include characters which otherwise have a different connotation in a java program (such as \ or “), escape sequences are used. An escape sequence begins with a backslash character (\), which indicates that the character(s) that follow should be treated in a special way. The output displayed by java can be formatted with the help of escape sequence characters.

The table displays the various escape sequences in java.

Escape Sequence

Description

\b

Backspace character

\t

Horizontal Tab Character

\n

New line character

\’

Single quote arks

\\

Backslash

\r

Carriage Return character

\”

Double quote marks

 

The code snippet demonstrates the use of escape sequence characters.

// use of tab and new line escape sequences System.out.println (“Java \t programming \n Language”) ;

// Printing Tom “Dick” Harry string           

System.out.println (“Tom \ “Dick\” Harry “);

Go through this video:-

https://www.youtube.com/watch?v=moQ3Kr8ouiU

READ MORE
...