Chapter 9: Access Modifiers & Constructors

Access Modifiers

Access Modifiers specify where a property/method is accessible.

There are four types of access modifiers in Java:

🔒 Private

Accessible only within the same class

📦 Default

Accessible within the same package

🛡️ Protected

Accessible within package and subclasses

🌐 Public

Accessible from anywhere

Modifier Class Package Subclass World
public
protected
default
private

Getters and Setters

Getter: Returns the value (Accessors)

Setter: Sets/Updates the value (Mutators)

EmployeeEncapsulation.java
public class Employee {
    private int id;
    private String name;
    private int salary;
    
    // Getter for name
    public String getName() {
        return name;
    }
    
    // Setter for name
    public void setName(String name) {
        this.name = name;
    }
    
    // Getter for id
    public int getId() {
        return id;
    }
    
    // Setter for id
    public void setId(int id) {
        this.id = id;
    }
    
    // Getter for salary
    public int getSalary() {
        return salary;
    }
    
    // Setter for salary with validation
    public void setSalary(int salary) {
        if (salary > 0) {
            this.salary = salary;
        } else {
            System.out.println("Salary must be positive!");
        }
    }
    
    public void displayInfo() {
        System.out.println("ID: " + id);
        System.out.println("Name: " + name);
        System.out.println("Salary: " + salary);
    }
}

Quick Quiz: Use these getters and setters from the main method

Constructors in Java

A member function used to initialize an object while creating it.

Constructor has the same name as the class name.

Without Constructor:

Employee harry = new Employee();
harry.setName("Harry Bhai");

With Constructor:

Employee harry = new Employee("Harry Bhai");
EmployeeWithConstructor.java
public class Employee {
    private int id;
    private String name;
    private int salary;
    
    // Default Constructor
    public Employee() {
        name = "Default Name";
        salary = 0;
        id = 0;
    }
    
    // Parameterized Constructor
    public Employee(String name) {
        this.name = name;
        this.salary = 0;
        this.id = 0;
    }
    
    // Constructor with multiple parameters
    public Employee(int id, String name, int salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }
    
    // Getters and Setters
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getSalary() {
        return salary;
    }
    
    public void setSalary(int salary) {
        this.salary = salary;
    }
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public void displayInfo() {
        System.out.println("ID: " + id + ", Name: " + name + ", Salary: " + salary);
    }
}

Constructor Overloading in Java

Constructors can be overloaded just like other methods in Java.

Notes:

  • Constructors can take parameters without being overloaded
  • There can be more than two overloaded constructors
  • TestConstructors.java
    public class TestConstructors {
        public static void main(String[] args) {
            // Using default constructor
            Employee emp1 = new Employee();
            emp1.displayInfo();
            
            // Using constructor with name parameter
            Employee emp2 = new Employee("Alice");
            emp2.displayInfo();
            
            // Using constructor with all parameters
            Employee emp3 = new Employee(103, "Bob", 75000);
            emp3.displayInfo();
        }
    }

    Quick Quiz: Overload the Employee constructor to initialize the salary to Rs 10,000

    Practice Set

    Chapter 9 - Practice Set

    1. Cylinder Class

    Create a class Cylinder and use getters and setters to set its radius and height.

    2. Surface Area & Volume

    Use problem 1 to calculate surface area and volume of the Cylinder.

    3. Constructor Implementation

    Use a constructor and repeat problem 1.

    4. Rectangle Constructor

    Overload a constructor used to initialize a rectangle of length 4 and breadth 5 for using custom parameters.

    5. Sphere Class

    Repeat problem 4 for a sphere.

    Sample Solution: Cylinder Class

    ← Previous: OOPs Next: Inheritance →