Categories
Java Tutorials

Array as private data member inside a Java class, access by getter and setter methods

Data hiding concept, private data member of a Java class described with the help of a Java array, where an array declared as private member inside a Java class. So you cannot access the array elements inside Java class directly. You can insert integers in the array, delete array elements (which are integers) and get element from the array in Java with the help of getter and setter methods. Deletion of array element needs shifting of elements because it creates a vacancy inside array.  A practical example of data hiding w.r.t array as private data member inside a Java class.

The source codes mentioned at the bottom of this tutorial article. The explanation of the program mentioned below. You can read the source code first and then follow the explanation mentioned below. This tutorial written by Core Java and Data structure course tutor from Kolkata India Mr Bikram Choudhury.

The description of this program

You cannot access the array elements directly. You have to access array elements via getter or setter methods of the class in which array has been declared as a private data member. The methods are public methods defined inside a class. The public methods listed below-

  • insertItem(int val) : Inserts an integer in the array
  • int getItem(int index) : Get an item , read an item from the array
  • delItem(int value) : Delete an item from the array
  • displayArray() : Display the elements of the array

Private data members array a[ ] and totalElements

In this program I have placed an array a[ ] inside a class named HiddenArrayInClass and declared the array as the private data member of the class. So in this program you cannot directly access the integer array element as a[index] and you have to access an array element a[index] using the public functions getItem(). index indicates any valid array index.

There exists another private data member called totalElements which tracks total no. of elements inside the array. HiddenArrayInClass object created in the class HiddenArrayInClassMain having main method in it.

About the constructor HiddenArryaInClass(size)

  • When inside the main() function defined within class HiddenArrayInClassMain  , you create an object of class HiddenArrayInClass ,the public constructor HiddenArryaInClass(size) is called automatically.
  • Inside this constructor an array a[ ] is instantiated with size of the array specified.
  • You need to specify the size of the array by passing an argument size which denotes the size of the array specified as the data member.
  • Also inside constructor function the data member of type integer totalElements initialized to zero, which denotes the total number of elements present in the array.
public HiddenArrayInClass(int size)
{
         a = new int[size];
         totalElements = 0;
}

because the array a[ ] has been declared as private member of the class HiddenArrayInClass , so you cannot access any of the array element a[index] directly using any array index n or index like obj.a[n] , where obj is considered an object of class HiddenArrayInClass .

The object of class  HiddenArrayInClass instantiated inside the class HiddenArrayInClassMain within main function body shown below.

public static void main(String[] args)
{
    HiddenArrayInClass obj = new HiddenArrayInClass(10);
}

Direct access of array elements is prohibitated

Because the array a[ ] is a private  data member so you cannot set the array element directly like

obj.a[ k ] = 19 

where k denotes array index variable. Also you cannot get an array element like this way

System.out.println(obj.a[ k ]);

Non-static data members and function members or methods of a class can be accessed w.r.t an object of the class, only if declared as public data member or public function.

The concept of data hiding

Insert an element inside the array in Java class

  • If you want to insert any integer like 100, or 200 or 300 then you need to use a public member function of HiddenArrayInClass class, to put an integer in the array hidden inside the class. In this program the function is insertItem().
  • The integer to be inserted inside the array (data member), is passed to this function as argument. The function should be called w.r.t an object of the class like obj.insertItem(200) then 200 will be placed inside the array.
  • So you don’t have the access of array elements directly.
  • By this way you can protect the array from getting directly accessed like obj.a[n]

Read the private array elements by using getItem(index) method

  • In the similar way if you want to retrieve the particular array element from the array, hidden inside the object (of the class) then you need to call getItem(n) method of the class.
  • The getItem(n) is a public method defined inside the class HiddenArrayInClass , so it can access the private members defined inside the class, because any method inside a class can access its private elements defined inside the same class. The getItem(n) method is called a getter method of this class.

The insertElement() is called the setter method, because by this method you can assign an array element.

Deletion of array element

  • It means removal of an array element, so when an element is deleted then there is a vacancy.
  • After deletion all the elements should be shifted to the left.
  • Say there are 10 elements in an array, then array index varies from 0 to 9. The array element at index 4 is to be deleted. So every array elements from index 5 to 9 should be shifted left.
  • After deletion, array a[5] should be placed in a[4], a[6] should be placed in a[5] … a[9] should be placed in a[8].

Deletion of array element needs shifting of elements because it creates a vacancy inside array.

Few words about setter method, private members in a Java Class

As the array a[ ] is the private member (data member) of the class HiddenArrayInClass shown below, a[ ] is the private data member of the class  also called instance variables. Only setter methods / constructors can assign values to the data members. By getter method you can access the “private data member” defined inside a class, here the class is HiddenArrayInClass.

HiddenArryaInClass.java

public class HiddenArrayInClass {

      private int[] a;
      private int totalElements;

      public HiddenArrayInClass(int size)
      {
         a = new int[size];
         totalElements = 0;
      }

      public void insertItem(int val)
      {
         a[totalElements] = val;
         totalElements++;
      }

      public int getItem(int index)
      {
        return a[index];
      }

      public void delItem(int value)
      {
        int i;
        for(i=0; i < totalElements ; i++ )
        {
          if(a[i] == value)
            break;
        }
        if( i == totalElements )
             System.out.println(value + 
                              " could not be deleted .. 
                              not found in array");
        else
        {
           for(int k = i; k < totalElements; k++)
                 a[k] = a[k+1];

           totalElements--;
           System.out.println(value + 
                     " deleted successfully");
           displayArray();
        }
     }

     void displayArray()
     {
         for(int i=0; i < totalElements; i++)
                System.out.print(a[i]+", ");
      
         System.out.println("");
     }
}

HiddenArrayInClassMain.java

public class HiddenArrayInClassMain
{
   public static void main(String[] args)
   {
   HiddenArrayInClass obj = new HiddenArrayInClass(10);
   obj.insertItem(200);
   obj.insertItem(300);
   obj.insertItem(400);
   obj.insertItem(500);
   obj.insertItem(1200);
   obj.insertItem(1300);
   obj.displayArray();
   obj.delItem(1500);
   obj.delItem(300);
   }

}