Thursday, 27 March 2014

default : no modifier

The data members, methods, types(includes classes, interfaces etc.,) which are not defined with any access specifier like private, protected and public all are come under default category.

The default data accessed with in the package, not outside.

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];
 
 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 myStack = new Stack();
  
  myStack.delete();
  
  myStack.insert(10);
  myStack.insert(20);
  
  myStack.display();
  System.out.println(myStack.getTop());
 }
}

When you tries to compile StackTest.java, compiler throws the below error






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment