×

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 Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure...

3 minutes read.

Java String isEmpty() method

Java String isEmpty() method checks whether current String is empty or not. Syntax: public boolean isEmpty() Returns It returns true, if length of String is 0 otherwise false. Java String isEmpty() method example 1    ...

1 minute read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Java String split() method

Java String split() method split current String against given regular expression and returns a char array. Syntax: public String split(String regex)                 public String split(String regex, int...

2 minutes read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 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.

Why are generics used in Java

Java has a feature called generics that allows you to make a class, interface, and function accepting any (reference) type as a parameter. In other words, it is the idea...

4 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

The final Keyword in Java

The final keyword is employed in several instances. Firstly, the non-access modifier final only applies to variables, methods, and classes. The final can be used in the following situations. Final Variables When...

6 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

Collections in Java

Collection framework is one of Java's most powerful subsystems. It is a set of classes and interfaces that is all-the-rage technology for superintending objects and a group of objects. In...

6 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.