Keyword in Java

The protected keyword in Java programming language

The protected keyword in Java programming language

In this tutorial, we will discuss about the protected keyword in java programming language

protected is a keyword in java programming language and an access modifier. This keyword can be used  to define a method or a variable of a class.

When a variable or a method is declared as protected, it can be accessed from

  • within the enclosing class
  • other class in the same package as in enclosing class
  • sub class , regardless of packages

Note – When a variable is marked as public, it can be used and accessed from enclosing class and outside classes

Variable(protected)

Syntax

Syntax of the protected keyword in Java variable

<access modifier> <Variable_type> <variable_name>;

Example

protected String stu_name; // this is a protected variable.  It can be invoked from other classes in the same package as its enclosing class.

Note – when a method is marked as protected, it can be executed from another class in the same package as in enclosing class

The protected keyword in Java

Program

Protected variable in same package

package sch;
public class school{
protected int marks;
}

The following class in the same package named as package sch;

package sch;
public class student{
void getMarks(){
school sc=new school();
sc.marks=56;  //access protected variable in same package
}
}

 

The following class in the different package. but it inherits (extends) studentclass  properties. Hence it can access the variable age directly.

package sch1;
import sch.school
public class student extends school{
void getmarks(){
school sc=new school();
sc.marks=56;  //you can access the protected variable in the 
                //different package of only from inherited class
}
}

Different package and no inheritance

following class is in different package you cannot access the variable marks directly

package sch1;
import sch.school
public class student_Two{
void getMarks(){
school sc=new school();
sc.marks=56;  //you cannot access the protected variable in the 
                           //different package of only from inherited class
}
}

Methods-(protected)

Syntax

Syntax of the protected key word in Java methods

<access modifier> <Variable_type> <method_name>();

Example

protected void run() {

}// this is a protected method. It can be invoked from the same package enclosing classes.

 

There are other Java language keywords that are similar to this keyword

 byte  in Java Language

 int  in Java Language

long  in Java Language

 float  in Java Language

char  in Java Language

 void  in Java Language

try  in Java Language

Catch in Java Language

implements  in java Language

public  in Java Language

static  in Java language

Volatile  in Java Language

Package  in Java Language

class  in Java Language

boolean  in Java Language

break in Java Language

do  in Java Language

double  in Java Language

This  in Java Language

final  in Java Language

switch  in Java Language

interface  in Java Language

extends  in Java language

 

Suggested for you

 Java language Keywords

C language Keywords

 Python language Keywords

keywords in C language

Finally  in Java language

The private keyword in Java programming language
The void keyword in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago