Back to the 2026 paper

Module 3: Methods of Java

20267m

In Java, a subclass needs to invoke a constructor and method of its superclass. Construct a suitable program using super and analyse the order in which superclass and subclass constructors execute.

Worked SolutionAI Assisted

Using super and constructor order

super(...) invokes a superclass constructor and must be the first statement in a constructor. super.method() calls an accessible superclass method implementation.

class Person {
    Person(String name) { System.out.println("Person: " + name); }
    void info() { System.out.println("Person info"); }
}
class Student extends Person {
    Student(String name) {
        super(name);
        System.out.println("Student created");
    }
    @Override void info() {
        super.info();
        System.out.println("Student info");
    }
}
class Demo {
    public static void main(String[] args) {
        Student s = new Student("Asha");
        s.info();
    }
}

Output order: Person: Asha, Student created, then Person info, Student info. The superclass constructor runs before the subclass constructor body.

Similar questions

Budget AppAndroid App

Pocketly on Android

Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.