Java

What is protected Keyword in Java

In Java, the protected keyword serves as an access modifier. It is utilized for constructors, methods, and variables. The protected keyword indicates that a field is only available inside the same package or through inheritance. d.

Usage of protected Keyword

  • Only other members of the same package may access a protected variable or method. However, inheritance also allows for access to them outside of the package. A subclass of this class has access to the parent class’s protected variables and methods.
  • It is not possible to declare a class or interface protected. For them, this access specifier is ineffective.
  • According to the guidelines for method overriding, a subclass that is overriding a protected method must use either a public or protected access modifier for the overriding method.

Java protected variable Example

Two classes in two separate packages are present here. We are attempting to access a class protected variable that is located in another package. This won’t compile since the protected access modifier forbids it.

//file 1: MyClass.java
package com.beginnersbook;
public class MyClass {
  protected String str="I am a protected variable of class MyClass";
}

//file 2: AnotherClass.java
package com.anotherpackage;
import com.beginnersbook.MyClass;

public class AnotherClass {
  public static void main(String[] args) {
    MyClass obj = new MyClass();
    System.out.println(obj.str);

  }
}

Output: Compilation error!

Let’s see how the output changes if we put these classes in the same package:

class MyClass {
  protected String str="I am a protected variable of class MyClass";
}
public class AnotherClass {
  public static void main(String[] args) {
    MyClass obj = new MyClass();
    System.out.println(obj.str);

  }
}

The protected variable may be accessed inside the same package, therefore the application works just fine.

I am a protected variable of class MyClass

Java protected method Example

The behavior is comparable to that of protected variable behavior. With the exception of situations when it is a subclass, it can only be accessed inside the same package.

class MyClass {
  protected void demo(){
    System.out.println("Protected method demo in class MyClass");
  }
}
public class JavaExample {
  public static void main(String[] args) {
    MyClass obj = new MyClass();
    obj.demo();
  }
}

Output:

Protected method demo in class MyClass

Java protected class Example

Java does not have the idea of a protected class, hence this program will fail to compile. In a similar vein, protected keywords cannot be used with interfaces.

protected class JavaExample {
  public static void main(String[] args) {
    JavaExample obj = new JavaExample();
  }
}

Output:

protected class example output

Java protected Constructor Example

There may be protected constructors in classes. A protected constructor class object cannot be created outside of the package.

We have two classes in the example that follows. The program runs without a hitch since they are in the same package. Class JavaExample uses a protected constructor to create an object of class MyClass.

//File 1: MyClass.java
class MyClass
{
  //protected constructor
  protected MyClass(){
    System.out.println("protected constructor of MyClass");
  }
}
//File 2: JavExample.java
class JavaExample {
  public static void main(String[] args) {
    MyClass obj = new MyClass();
  }
}

Output:

protected constructor of MyClass

Overriding a protected method in subclass

In the sub class, a protected method may be overridden; only keep in mind the following guideline:

  • You may only set a subclass’ overriding method’s access modifier to public or protected; otherwise, you will get the compilation error “cannot limit the visibility of the inherited method.”
  • This is so that subclass methods. cannot be more limiting. Because they are more restricted than protected, the default and private access modifiers cannot be utilized in this situation
//File 1: MyClass.java
class MyClass
{
  //protected method in parent class
  protected void demo()
  {
    System.out.println("Protected method demo of MyClass");
  }
}
//File 2: JavExample.java
class JavaExample extends MyClass {
  //overriding the demo() method
  protected void demo()
  {
    System.out.println("Overriding method demo");
  }
  public static void main(String[] args) {
    JavaExample obj = new JavaExample();
    obj.demo();
  }
}

Output:

Overriding method demo

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button