En este momento estás viendo ▷▷ [Solved] How will a class protect the code inside it?

▷▷ [Solved] How will a class protect the code inside it?

How will a class protect the code inside it?

The protected keyword in Java refers to one of its access modifiers. The methods or data members declared as protected can be accessed from

How will a class protect the code inside it?

How will a class protect the code inside it? Explanation: Each method or variable in a class may be marked ‘public’ or ‘private’. They are called Access Specifiers. 6.

  • Within the same class.
  • Subclasses of the same packages.
  • Different classes of the same packages.
  • Subclasses of different packages.

There are some certain important points to be remembered as follows: 

  1. If one wishes to access a protected modifier outside a package, then inheritance is needed to be applied.
  2. Protecting a constructor prevents the users from creating the instance of the class, outside the package.
  3. During overriding, when a variable or method is protected, it can be overridden to other subclass using either a public or protected modifier only.
  4. Outer class and interface cannot be protected.

Implementation: Here we will be creating two packages p1 and p2. Class A in p1 is made public, to access it in p2. The method displayed in class A is protected and class B is inherited from class A and this protected method is then accessed by creating an object of class B.

Example 1: Package p1 

  • Java
// Java program to illustrate // protected modifier  packagep1;  // Class A publicclassA {     protectedvoiddisplay()             System.out.println("GeeksforGeeks");     }

 
Package p2 

  • Java
// Java program to illustrate// protected modifier packagep2; // import all classes in package p1importp1.*; // Class B is a subclass of AclassB extendsA {    publicstaticvoidmain(String args[])    {        B obj = newB();        obj.display();    }}

Output: 

GeeksforGeeks

Now let us try to analyze different conditions of access: 

  1. Calling protected function without extending the parent class
  2. Accessing a protected class
  3. Accessing display function from the same package but different 
  4. Accessing display function from a different package
  5. Accessing a protected class by overriding to sub-class within the same package

A. Calling Protected Function Without Extending the Parent Class

Here we will create two packages p1 and p2. Class A in p1 is made public, to access it in p2. The method displayed in class A is protected. But the code will not be able to access the function “display” since the child class has not inherited its value from the main class and will throw an exception as shown. 

Example 1-A: Package p1 

  • Java
// Java program to illustrate Protected Modifier packagep1; // Class ApublicclassA {     // Method    protectedvoiddisplay()    {        // Print statement        System.out.println("GeeksforGeeks");    }}

Example 1-B: Package p2 

  • Java
// Java program to illustrate// protected modifier packagep2; // import all classes in package p1importp1.*; // Class B is a subclass of AclassB {    publicstaticvoidmain(String args[])    {        B obj = newB();        obj.display();    }}

Output: 

Exception in thread "main" 
java.lang.RuntimeException: 
Uncompilable source code - 
Erroneous sym type: p2.B.display
    at p2.B.main(B.java:16)

B: Accessing a Protected Class

Here we are trying to access a protected class A resulting in an error. 

Example A:

  • Java
// Java program to illustrate// protected modifier packagep1; // Class AprotectedclassA {    voiddisplay()    {        System.out.println("GeeksforGeeks");    }}

Example B: Package p2 

  • Java
// Java program to illustrate// protected modifier packagep2; // import all classes in package p1importp1.*; // Class B is a subclass of AclassB extendsA {    publicstaticvoidmain(String args[])    {        B obj = newB();        obj.display();    }}

Output: This will throw an error

Exception in thread "main" 
java.lang.RuntimeException: 
Uncompilable source code - 
Erroneous sym type: p2.B.display
    at p2.B.main(B.java:16)

C: Accessing Display Function From the Same Package But Different Class

Implementation: In this example, we have access to access a protected function “display” from the same package but a different class 

Example A: Package p1 

  • Java
// Java program to illustrate// protected modifier packagep1; // Class ApublicclassA {    protectedvoiddisplay()    {        System.out.println("GeeksforGeeks");    }}

Example B: class C 

  • Java
// Java program to illustrate// protected modifier // Class C is a subclass of ApublicclassC {    publicstaticvoidmain(String args[])    {        A obj = newA();        obj.display();    }}

Output: 

GeeksforGeeks

D: Accessing Display Function From a Different Package

Here we have tried to access the protected function display from a different package by inheritance and extending the class. 

Example A: Package p1 

  • Java
// Java program to illustrate// protected modifier packagep1; // Class ApublicclassA {    protectedvoiddisplay()    {        System.out.println("GeeksforGeeks");    }}

Example B: Package p2 

  • Java
// Java program to illustrate// protected modifier packagep2; // import all classes in package p1importp1.*; // Class B is a subclass of AclassB extendsA {    publicstaticvoidmain(String args[])    {        B obj = newB();        obj.display();    }}

Output: 

GeeksforGeeks

E: Accessing a Protected Class by Overriding to Sub-Class Within the Same Package

Here we have designed two classes A and C, where class C is the overridden one. 

Example A: class A 

  • Java
// Java program to illustrate// protected modifierpackagep1; // Class ApublicclassA {    protectedvoiddisplay()    {        System.out.println("Class A");    }}

Example B: class C 

  • Java
// Java program to illustrate// protected modifier publicclassC extendsA {     // overridden function    protectedvoiddisplay()    {        System.out.println("Class C");    }    publicstaticvoidmain(String args[])    {        C obj1 = newC();        obj1.display();    }}

Output: 

Class C

En Estrategia Creativa tenemos toda la información que necesitas sobre el mundo de digital y en especial para tus emprendimiento Si quieres seguir leyendo información útil  ¡echa un vistazo a nuestros artículos

Nuestra puntuación

Deja una respuesta