Menu
Topics Index
...
`


Interfaces, Packages and Access Control >
Siva Nookala - 20 Feb 2017
Access Control is a way of preventing the misuse of data belonging to a class. The following program shows how the data can be corrupted or can become inconsistent.

Test Student Marks
class TestStudentMarks
{
    public static void main(String arg[])
    {
        Student madhavan = new Student("Madhavan");
        
        madhavan.subject1 = 87;
        madhavan.subject2 = 65;
        madhavan.subject3 = 93;
        
        madhavan.total_marks = 235;
        
        System.out.println("Subject 1 = " + madhavan.subject1);
        System.out.println("Subject 2 = " + madhavan.subject2);
        System.out.println("Subject 3 = " + madhavan.subject3);
        System.out.println("Total = " + madhavan.total_marks);    
    }
}

class Student
{
    String name;

    Student(String name)
    {
        this.name = name;
    }

    int subject1;
    int subject2;
    int subject3;
    int total_marks;
}
OUTPUT

Subject 1 = 87
Subject 2 = 65
Subject 3 = 93
Total = 235

DESCRIPTION

Here we have created a Student class and with member variables subject1, subject2, subject3 and the total_marks. The marks of various subjects and the total is initialized and then they are printed.

If we carefully observe the above program, we will notice that the total_marks is 235 instead of 245 (= 87 + 65 + 93). This is obviously wrong and the data became inconsistent.
The above program can be rewritten as shown below to avoid these inconsistencies.
Test Student Marks Using Private Keyword
class TestStudentMarksImproved
{
    public static void main(String arg[])
    {
        Student madhavan = new Student("Madhavan");
        
        madhavan.setMarks(87, 65, 93);
        
        System.out.println("Subject 1 = " + madhavan.getSubject1Marks());
        System.out.println("Subject 2 = " + madhavan.getSubject2Marks());
        System.out.println("Subject 3 = " + madhavan.getSubject3Marks());
        System.out.println("Total = " + madhavan.getTotalMarks());
        
        
        // LINE X - THROWS COMPILATION ERROR
        // madhavan.subject1 = 87;
        // LINE Y - THROWS COMPILATION ERROR
        // System.out.println("Subject 1 = " + madhavan.subject1);
        
    
    }
}

class Student
{
    private String name;
    private int subject1;
    private int subject2;
    private int subject3;
    private int total_marks;

    Student(String name)
    {
        this.name = name;
    }

    void setMarks(int subject1, int subject2, int subject3)
    {
        this.subject1 = subject1;
        this.subject2 = subject2;
        this.subject3 = subject3;

        this.total_marks = subject1 + subject2 + subject3; // LINE A
    }

    int getSubject1Marks()
    {
        return subject1; // LINE B
    }

    int getSubject2Marks()
    {
        return subject2;
    }

    int getSubject3Marks()
    {
        return subject3;
    }

    int getTotalMarks()
    {
        return total_marks;
    }
}
OUTPUT

Subject 1 = 87
Subject 2 = 65
Subject 3 = 93
Total = 245

DESCRIPTION

In this program we have used the private keyword. private is an access specifier which prevents a member variable to be accessed from outside the class. e.g., The member variable subject1 is only accessible inside the class Student. If we try to access it from main method in the class TestStudentMarksImproved, it will throw a compilation error.

THINGS TO TRY
  • Uncomment the LINE X and LINE Y and observe the compilation error you get.
  • After uncommenting the LINE X and LINE Y and also remove the private access specifier for subject1 and see that the program compiles with out any error.
Although the modified program became bigger, it is highly secured compared to its previous version. This way of protecting the data from the other classes is called Encapsulation. Encapsulation is one of the three object oriented concepts, the other two being inheritance and polymorphism.
Also note that the keyword private is an access specifier and it is the most strict form of access. A variable marked as private can only be accessed in that class. It can not be accessed even in its sub-classes. Java supports three other levels of access and they are explained in Access Modifiers In Java.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.