TutorialsICSEClass 10Computer Applications

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10

ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.

10 min read
Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10

"Encapsulation" and "access specifiers" are favourite 2-mark questions in Section A of the ICSE Computer Applications paper. Questions on instance vs static variables, and on the scope of variables, often appear next to them. These notes cover the whole chapter, with one complete program that uses everything.

What is encapsulation?

Encapsulation is the wrapping of data (variables) and the methods that work on that data into a single unit, the class. It is closely linked to data hiding: the data is kept private, and it can only be reached through the class's own methods.

Why use it?

  • It protects the data from being changed in invalid ways (for example, a negative balance).
  • The class controls how its data is read and updated.
  • The inner details can change without affecting the code that uses the class.

Access specifiers

Access specifiers control where a member can be used.

  • private: usable in the same class only.
  • default (no keyword): usable in the same class and the same package.
  • protected: usable in the same class, the same package, and subclasses in other packages.
  • public: usable everywhere.

From most to least restrictive: private → default → protected → public.

Getters and setters

With private data, the class provides public methods to read the value (getters, or accessors) and to change it (setters, or mutators). A setter can check the new value first:

class Student {
    private int marks;

    public int getMarks() {             // getter (accessor)
        return marks;
    }

    public void setMarks(int m) {        // setter (mutator)
        if (m >= 0 && m <= 100)
            marks = m;
        else
            System.out.println("Invalid marks");
    }
}

Instance variables vs static (class) variables

  • An instance variable has a separate copy in each object. It is created when the object is created.
  • A static variable has one copy shared by all objects of the class. It is created once, when the class is loaded.
  • An instance variable is reached through an object (obj.var); a static variable is reached through the class name (ClassName.var).
  • Example: each student's name is an instance variable, while schoolName (the same for everyone) is a static variable.
class Student {
    String name;                  // instance variable
    static int count = 0;         // class (static) variable

    Student(String n) {
        name = n;
        count++;                  // shared by every object
    }
}

new Student("Riya");
new Student("Aarav");
System.out.println(Student.count);   // 2

Scope of variables

The scope of a variable is the part of the program where it can be used.

  • Local variables are declared inside a method or block. They exist only inside that block and have no default value, so they must be initialised before use.
  • Parameters (formal parameters) work like local variables for the whole method.
  • Instance and static variables are declared in the class, outside every method. They can be used in all the methods of the class and get default values (0, false or null).
class ScopeDemo {
    int x = 10;                      // instance variable: whole class

    void show() {
        int y = 5;                   // local: only inside show()
        for (int i = 0; i < 2; i++) {
            // i exists only inside this loop
            System.out.println(x + y + i);
        }
        // System.out.println(i);    // ERROR: i is out of scope here
    }
}

Solved program: BankAccount

class BankAccount {
    private String holder;
    private double balance;
    static int accounts = 0;

    BankAccount(String h, double b) {
        holder = h;
        balance = b;
        accounts++;
    }

    public void deposit(double amt) {
        if (amt > 0)
            balance += amt;
    }

    public void withdraw(double amt) {
        if (amt > 0 && amt <= balance)
            balance -= amt;
        else
            System.out.println("Insufficient balance or invalid amount");
    }

    public double getBalance() {
        return balance;
    }

    public static void main(String args[]) {
        BankAccount a = new BankAccount("Riya", 5000);
        a.deposit(1500);
        a.withdraw(8000);                        // rejected
        a.withdraw(2000);
        System.out.println("Balance: " + a.getBalance());   // 4500.0
        System.out.println("Accounts opened: " + BankAccount.accounts);  // 1
    }
}

balance is private, so it can only change through deposit() and withdraw(), which reject invalid amounts. That is encapsulation in practice.

Trace: 5000 + 1500 = 6500. Withdrawing 8000 is rejected. Withdrawing 2000 leaves 4500.0.

Practice questions

  1. Name the most restrictive access specifier.
  2. What is the difference between a local variable and an instance variable?
  3. Why is data usually declared private in a class?

Answers:

  1. private.
  2. A local variable is declared inside a method, exists only there and has no default value. An instance variable is declared in the class, belongs to each object and gets a default value.
  3. To hide it from outside code, so it can only be changed through the class's own methods, which can check that each change is valid. This is encapsulation.

Revise Constructors in Java and User-Defined Methods, then try the class-based programs in the ICSE Class 10 Sample Paper 2027.