×

Java protected vs private

Java :

Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems in the year 1996 as java 1.0 . It is a high level, robust, secured programming language.

Java consists of Java Virtual Machine (JVM) which uses the execution engine and converts the class file into executable file (.class file to .exe file). The execution engine consists of Interpreter and Just In Time(JIT) compiler. In Java we will create classes and then we will create objects.

There are many kind of key words , data types , operators and also the access specifiers in java.

Access Specifiers :

Access specifiers are also known as access modifiers. Access specifiers are the specifiers which are used to access the certain variables , methods , class and also the constructor based on specific conditions or specific constraints. The access specifiers or access modifiers are the keywords in java.

The constraints or conditions which are specified are known as access levels and also as access control. We can change the access levels by using different kind of access specifiers or access modifiers. It is also known as modifying the access levels or access control.

There are mainly three kind of access specifiers or access modifiers. But there are four kinds of access control or access levels. The one which is an access control but not access specifier is default. The default is an access control that will be activated immediately if we do not use any kind of access specifier or access modifier.

The different kinds of access specifier or access modifier are as shown below :

1) Private

2) Protected

3) Public

4) Default

Now let us look over the access control of each and every access specifier.

1) Private : The control of private access specifier is in class level.

2) Protected : The control of protected access specifier is in package level and also in sub class level.

3) Public : If we use the public access specifier then there will be visibility to every one.

4) Default : The control of default key word is in the package level only.

The above shown are the different types of access specifiers that are used in the java programming. Now let us know about the private and protected access specifiers or access modifiers more in detail.

Private :

The private key word is an access specifier or access modifier. It is used in access control or access level. We can access a method or variable or constructor within the particular class level by using the private access specifier in front of the variable or method or constructor.

The private access specifier or access modifier is only applicable to constructor , variable ( instance variable ) , method.

The private access specifier or access modifier is not applicable to class , interface , variable ( local variable ).

By using the private key word the visibility is only within the class we cannot access outside of the class by using the private access specifier or access modifier.

Syntax for using private access specifier :

private variable;
   ( or )
private constructor ( )
{
}
   ( or )
private method ( )
{
}

Now let us see an example with the following code about the private keyword as shown below:

Example:

package a;
class Person
{
private void show ( )
{
System.out.println( “ Welcome to java “ );
} // show
} // person class
Class Demo
{
public static void main ( String args [ ] )
{
Person obj = new Person ( );
obj.show ( );
} // main method
} // demo class

Output:

Error : show ( ) has private access in Person
              obj.show ( );

Protected:

The protected key word is an access specifier or access modifier. It is used in access control or access level. We can access a method or variable or constructor within the particular package level and sub class level by using the protected access specifier in front of the variable or method or constructor.

The protected access specifier or access modifier is only applicable to constructor , variable ( instance variable ) , method.

The protected access specifier or access modifier is not applicable to class , interface , variable ( local variable ).

Syntax for using protected access specifier :

protected variable;
   ( or )
protected constructor ( )
{
}
   ( or )
protected method ( )
{
}

Now let us see an example with the following code about the protected keyword as shown below :

Example :

package a;
public class Person
{
protected void show ( )
{
System.out.println( “ Welcome to java “ );
} // show
} // person class
package b;
import a.*;
class Demo extends Person
{
Public static void main ( String args [ ] )
{
Demo obj = new Demo ( );
obj.show ( );
} // main method
} // demo class

Output :

Welcome to java

Related Topics

Java While Keyword

Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. The while loop can be viewed as an iterative version of the if...

3 minutes read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

Activity selection problem in Java

The activity selection problem is a multiple objective problem hat requires choosing non-conflicting tasks to complete within a specific amount of time from a list of tasks identified by a...

5 minutes read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 minutes read.

Java Program to Create Set of Pairs Using HashSet

HashSet in Java: HashSet is an assortment in Java that has a place with java.util bundle. It inside utilises HashMap to store components. It is an execution of the Set connection...

11 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Java Arrays

An array is an object that stores a collection of values. An array can store two types of collection data: objects and primitives. An array is a collection of values which...

2 minutes read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

LCM Program in Java

LCM Program in Java The LCM program in Java outputs the LCM of the given numbers. In Arithmetic, the lowest common multiple, least common multiple or smallest common multiple of the...

6 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

How to take Multiple String Input in Java using Scanner class

Scanner class is a class which takes the multiple input data or the single input data through an objects and methods. The Scanner class will be available in the java.util...

3 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

Sudoku in Java

Sudoku is a combinatorial-number-placement puzzle with a logic-based approach. The goal of a traditional Sudoku puzzle is to fill in the numbers on a 9 by 9 grid so that...

6 minutes read.

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.