Thursday 27 March 2014

protected access specifier


Above diagram shows the directory structure.
  1. Directory named “data_structure” contains two sub directories linear and non-linear.
  2. Directory linear contains two java files named Stack.java, DataStructure.java
Any member which is declared as protected is accessible with in the package and subclasses of a class.


package data_structure.linear;

public interface DataStructure{
 int MAX_SIZE = 1000;
 
 void display();
 void insert(int data);
 void delete();
 int search(int data);
}



package data_structure.linear;

public class Stack implements DataStructure{
 int top = -1;
 
 int stack[] = new int[DataStructure.MAX_SIZE];
 
 protected int getTop(){
  return top;
 }
 
 public void display(){
  for(int i=0; i<=top; i++){
   System.out.println(stack[i]);
  }
 }
 
 public void insert(int data){
  if(top < DataStructure.MAX_SIZE-1){
   top++;
   stack[top] = data;
  }
  else{
   System.out.println("Stack is not empty");
  }
 }
 
 public void delete(){
  if(top == -1){
   System.out.println("Stack is Empty");
  }
  else{
   stack[top] = 0;
   top--;
  }
 }
 
 public int search(int data){
  for(int i=0; i < top; i++)
   if(stack[i] == data)
    return 0;
  return -1;
 }
}


package data_structure;

import data_structure.linear.*;
class StackTest{
 public static void main(String args[]){
  Stack myStack1 = new Stack();
  
  myStack1.delete();
  
  myStack1.insert(10);
  myStack1.insert(20);
  
  myStack1.display();
  System.out.println(myStack1.getTop());
 }
}

Statements to execute the StackTest.java

C:\>javac -cp . data_structure\linear\DataStructure.java

C:\>javac -cp . data_structure\linear\Stack.java

C:\>javac -cp . data_structure\StackTest.java

C:\>java -cp . data_structure.StackTest
data_structure\StackTest.java:14: error: getTop() has protected access in Stack
System.out.println(myStack1.getTop());
^
1 error

As you observe the output, compiler throws the error, since getTop is protected, so it can accessible from within the package or the subclasses of the Stack class.

To make the program runs fine make the StackTest class to extend the Stack class.


package data_structure;

import data_structure.linear.*;

class StackTest extends Stack{

 public static void main(String args[]){
  StackTest myStack1 = new StackTest();
  
  myStack1.delete();
  
  myStack1.insert(10);
  myStack1.insert(20);
  
  myStack1.display();
  System.out.println(myStack1.getTop());
 }
}
Output
C:\>javac -cp . data_structure\StackTest.java

C:\>java -cp . data_structure.StackTest
Stack is Empty
10
20
1

Some Points to Remember
1. Java don't allow protected classes and protected interfaces
Protected class member (method or variable) is just like package-private (default visibility), except that it also can be accessed from subclasses.
Since there's no such concept as 'subpackage' or 'package-inheritance' in Java, declaring class protected or package-private would be the same thing. But Java supports protected nested or inner classes


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment