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