×

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

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

Java For Loop

A for loop is used to execute a set of statements for a fixed number of times. It takes the following form: for (initialization; condition; update) { statements; } The for loop defines...

2 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract...

3 minutes read.

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Sphenic Number in Java

In this section, we will learn what is a sphenic number is and show you how to write Java programmes to determine if a specific number are sphenic or not....

3 minutes read.

This Operator Using in Java

this keyword in Java has a wide range of applications. this reference variable in Programming language refers to the object of interest. This keyword is used in Java. this keyword is...

5 minutes read.

Package naming convention in Java

It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose...

4 minutes read.

Java Switch string

A multi-way branch statement is the switch statement. It offers a simple method for allocating execution to various code sections according to the expression's value. Primitive data types, including bytes,...

3 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Vector in Java

Java Vector Class The Vector class is a legacy class that implements a growable array of objects. The components that it contains can be accessed using an integer index. The size of...

26 minutes read.

Java try catch

Java try catch There can be statements that can cause exceptions, and the exception leads to abnormal termination of the program. To avoid that abnormal termination, those statements that look potent...

4 minutes read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.