TutorialsICSEClass 10Computer Applications

Constructors in Java: ICSE Class 10 Notes with Types, Overloading and Programs

ICSE Class 10 Computer Applications notes on constructors: what a constructor is, default and parameterised constructors, constructor overloading, the this keyword, differences from methods, and solved programs.

11 min read
Constructors in Java: ICSE Class 10 Notes with Types, Overloading and Programs

Almost every class-based program in the ICSE Computer Applications board paper asks you to write a constructor, and 2-mark theory questions on constructors appear every year. These notes explain the chapter from scratch, with programs you can run in BlueJ.

What is a constructor?

A constructor is a special member method that initialises an object when it is created. It has three defining features:

  1. It has the same name as the class.
  2. It has no return type, not even void.
  3. It is called automatically when an object is created with new.
class Book {
    String title;
    double price;

    Book() {                 // constructor
        title = "Untitled";
        price = 0.0;
    }
}

Book b = new Book();          // constructor runs automatically here

Types of constructors

1. Default (non-parameterised) constructor

A default constructor takes no parameters and gives the data members starting values. If you do not write any constructor, Java supplies one automatically. That built-in constructor sets the data members to their default values: 0 for numbers, false for boolean, null for objects.

2. Parameterised constructor

A parameterised constructor receives values as parameters and uses them to initialise the object.

class Book {
    String title;
    double price;

    Book(String t, double p) {
        title = t;
        price = p;
    }
}

Book b = new Book("Java Basics", 250.0);
Once you write a parameterised constructor, Java no longer supplies the default one. new Book() will then cause a compile error unless you also write Book() yourself.

Constructor overloading

A class can have more than one constructor, as long as their parameter lists differ in the number, type or order of parameters. This is called constructor overloading.

class Rectangle {
    int length, breadth;

    Rectangle() {                    // default
        length = breadth = 1;
    }
    Rectangle(int side) {            // square
        length = breadth = side;
    }
    Rectangle(int l, int b) {        // rectangle
        length = l;
        breadth = b;
    }
    int area() {
        return length * breadth;
    }
}

new Rectangle() gives area 1, new Rectangle(5) gives 25, and new Rectangle(4, 6) gives 24. Java chooses the constructor whose parameters match the arguments.

The this keyword

this refers to the current object. It is used when a parameter has the same name as a data member:

class Student {
    String name;
    int roll;

    Student(String name, int roll) {
        this.name = name;     // this.name is the data member
        this.roll = roll;     // roll alone is the parameter
    }
}

Without this, name = name; would just assign the parameter to itself, and the data member would stay null.

Constructor vs method

  • Name: a constructor has the same name as the class; a method can have any valid name.
  • Return type: a constructor has none; a method must have one (or void).
  • When it runs: a constructor runs automatically when an object is created; a method must be called explicitly.
  • Purpose: a constructor initialises the object; a method performs an action.

Solved board-style program

Question: Define a class ElectricBill with the data members name and units and a parameterised constructor. A method calculate() computes the bill: ₹2 per unit for the first 100 units, ₹3 per unit for the next 200 units, and ₹5 per unit above 300 units. A method print() displays the details.

import java.util.Scanner;

class ElectricBill {
    String name;
    int units;
    double bill;

    ElectricBill(String n, int u) {
        name = n;
        units = u;
        bill = 0.0;
    }

    void calculate() {
        if (units <= 100)
            bill = units * 2;
        else if (units <= 300)
            bill = 100 * 2 + (units - 100) * 3;
        else
            bill = 100 * 2 + 200 * 3 + (units - 300) * 5;
    }

    void print() {
        System.out.println("Name: " + name);
        System.out.println("Units: " + units);
        System.out.println("Bill: Rs. " + bill);
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter name: ");
        String n = sc.nextLine();
        System.out.print("Enter units: ");
        int u = sc.nextInt();
        ElectricBill ob = new ElectricBill(n, u);
        ob.calculate();
        ob.print();
    }
}

Sample run: for 350 units, the bill is 200 + 600 + 250 = Rs. 1050.0.

Practice questions

  1. Can a constructor be declared private? Can it return a value?
  2. What happens if a class has only a parameterised constructor and you write new ClassName()?
  3. Write two overloaded constructors for a class Circle: one with no parameters (radius 1) and one that takes the radius.

Answers:

  1. Yes, it can be private, which is used to restrict object creation. No, a constructor never returns a value.
  2. A compile-time error, because Java no longer supplies a default constructor.
  3. Circle() { r = 1; } and Circle(double r) { this.r = r; }

Test yourself with our ICSE Class 10 MCQs on Methods and Constructors, which come with answers.