TutorialsISCClass 12Computer Science

Inheritance in Java for ISC Class 12: super, Method Overriding and Solved Programs

ISC Class 12 Computer Science notes on inheritance in Java: superclass and subclass, the extends and super keywords, protected members, method overriding, types of inheritance, and a fully solved board-style program.

12 min read
Inheritance in Java for ISC Class 12: super, Method Overriding and Solved Programs

Section C of the ISC Computer Science theory paper regularly has an inheritance question: you are given a superclass and asked to write the subclass. It is a scoring question once you know the pattern. These notes explain every part of it.

What is inheritance?

Inheritance lets a new class (the subclass, or derived class) acquire the data members and methods of an existing class (the superclass, or base class). The subclass can reuse that code and add or change behaviour.

class Person {                     // superclass
    protected String name;
    protected int age;
}

class Student extends Person {     // subclass
    int roll;                      // new member
}

A Student object now has name, age and roll.

Access specifiers in inheritance

  • private: accessible only inside its own class. Not accessible in the subclass.
  • protected: accessible in the subclass, and in the same package.
  • public: accessible everywhere.
In ISC questions, the superclass members are usually declared protected so the subclass can use them directly.

The super keyword

super has two uses.

1. Calling the superclass constructor. This must be the first statement in the subclass constructor:

class Student extends Person {
    int roll;
    Student(String n, int a, int r) {
        super(n, a);        // initialises name and age in Person
        roll = r;
    }
}

2. Calling a superclass method that the subclass has overridden:

void display() {
    super.display();                      // prints name and age
    System.out.println("Roll: " + roll);  // adds the subclass details
}

Method overriding

Overriding means the subclass defines a method with the same name, return type and parameters as a method in the superclass. When the method is called on a subclass object, the subclass version runs.

Overloading vs overriding

  • Where: overloading happens in the same class (or a subclass); overriding needs a superclass and a subclass.
  • Parameters: overloaded methods must have different parameters; an overriding method must have the same parameters.
  • Decided at: overloading is resolved at compile time; overriding is resolved at run time.

Types of inheritance in Java

  • Single: A → B
  • Multilevel: A → B → C
  • Hierarchical: A → B and A → C

Java does not allow multiple inheritance of classes (class C extends A, B is invalid). The same effect is achieved with interfaces.

Solved board-style program

Question: A superclass Product has the data members name and price, a parameterised constructor, and a method void show() that displays them. Define a subclass Sale with:

  • a data member discountPct (the discount percentage)
  • a parameterised constructor that also initialises the superclass members
  • double finalPrice(), which returns the price after the discount
  • an overridden void show() that displays all the details and the final price

Assume Product is already defined. Write the class Sale only.

class Sale extends Product {
    double discountPct;

    Sale(String n, double p, double d) {
        super(n, p);              // initialise name and price
        discountPct = d;
    }

    double finalPrice() {
        return price - price * discountPct / 100;
    }

    void show() {
        super.show();             // name and price from Product
        System.out.println("Discount: " + discountPct + "%");
        System.out.println("Final price: " + finalPrice());
    }
}

For a product priced ₹1200 with a 15% discount, finalPrice() returns 1020.0.

Write only the class that is asked for. In this type of question, writing main() or rewriting the superclass earns no extra marks and wastes time.

Checklist for inheritance questions

  1. Write class Sub extends Super.
  2. Call super(...) as the first line of the constructor, passing the superclass values.
  3. Use the superclass's protected members directly. Do not redeclare them in the subclass.
  4. When overriding the display method, call super.show() (or the method the question names) first, then print the new members.

Practice questions

  1. What is the output if a subclass constructor does not call super(...) and the superclass has only a parameterised constructor?
  2. Can a subclass access a private member of its superclass directly?
  3. State one difference between this and super.

Answers:

  1. A compile-time error. Java tries to call the default super(), which does not exist.
  2. No. It can only use it through public or protected methods of the superclass.
  3. this refers to the current object, while super refers to the superclass part of the object (its constructor and methods).

Continue with our ISC Class 12 Recursion Programs and practise in the solved ISC Class 12 Computer Science Sample Paper 2027.