TutorialsISCClass 12Computer Science

Abstract Classes and Interfaces in Java for ISC Class 12 (with Programs)

ISC Class 12 Computer Science notes on abstract classes and interfaces in Java: abstract methods, implementing interfaces, constants in interfaces, multiple interfaces, the key differences, and a solved board-style program.

11 min read
Abstract Classes and Interfaces in Java for ISC Class 12 (with Programs)

After inheritance, the next step in ISC Class 12 Java is abstraction: describing *what* a class must do without saying *how*. Java gives you two tools for this, abstract classes and interfaces. Both appear in theory questions and in Section C programs.

Abstract methods and abstract classes

An abstract method has a heading but no body. It ends with a semicolon:

abstract double area();

A class that contains an abstract method must be declared abstract:

abstract class Shape {
    String name;

    Shape(String n) {
        name = n;
    }

    abstract double area();        // no body: every subclass must define it

    void show() {                  // concrete method: shared by all subclasses
        System.out.println(name + " area = " + area());
    }
}

Key rules:

  • You cannot create an object of an abstract class: new Shape("x") is an error.
  • A subclass must override every abstract method, or be declared abstract itself.
  • An abstract class can have constructors, data members and normal (concrete) methods.
class Square extends Shape {
    double side;

    Square(double s) {
        super("Square");
        side = s;
    }

    double area() {
        return side * side;
    }
}

new Square(4).show() prints Square area = 16.0.

Interfaces

An interface is a completely abstract type: a list of methods that a class promises to provide.

interface Payable {
    double TAX_RATE = 0.18;           // constant: public static final by default
    double amountDue();               // abstract: public abstract by default
}
  • The methods in an interface are public and abstract by default. (Since Java 8, interfaces can also have default and static methods, but ISC questions focus on abstract methods.)
  • Variables in an interface are constants: automatically public static final.
  • A class uses an interface with the keyword implements, and must define all its methods.
class Invoice implements Payable {
    double price;

    Invoice(double p) {
        price = p;
    }

    public double amountDue() {       // must be public
        return price + price * TAX_RATE;
    }
}

new Invoice(1000).amountDue() returns 1180.0.

The implementing method must be declared public, because interface methods are public. Leaving out public is a common compile error.

Multiple interfaces

A class can extend only one class, but it can implement many interfaces. This is how Java achieves the effect of multiple inheritance:

class SmartPhone extends Device implements Camera, MusicPlayer {
    // must define all the methods of Camera and MusicPlayer
}

Abstract class vs interface

  • Methods: an abstract class can have both abstract and concrete methods; interface methods are abstract by default.
  • Variables: an abstract class can have any instance variables; an interface can only have constants (public static final).
  • Constructors: an abstract class can have them; an interface cannot.
  • Keyword: a class extends an abstract class but implements an interface.
  • How many: a class can extend only one class, but can implement many interfaces.

Solved board-style program

Question: An interface Data has a constant PI = 3.14 and a method double volume(). Define a class Cylinder that implements Data, with the data members radius and height, a parameterised constructor, the method volume() that returns πr²h, and a method display() that prints the radius, the height and the volume.

interface Data {
    double PI = 3.14;
    double volume();
}

class Cylinder implements Data {
    double radius, height;

    Cylinder(double r, double h) {
        radius = r;
        height = h;
    }

    public double volume() {
        return PI * radius * radius * height;
    }

    void display() {
        System.out.println("Radius: " + radius);
        System.out.println("Height: " + height);
        System.out.println("Volume: " + volume());
    }
}

For radius 2 and height 5, the volume is 3.14 × 4 × 5 = 62.8. Java may print a tiny rounding difference in the last decimal place, because double arithmetic is not exact.

Practice questions

  1. Can an abstract class have a constructor? Why would it need one?
  2. What is the default access of a method declared in an interface?
  3. Why does Java not allow class C extends A, B?

Answers:

  1. Yes. It initialises the common data members, and subclasses call it with super(...).
  2. public (and abstract).
  3. To avoid ambiguity when both A and B have a method with the same signature (the "diamond problem"). Java uses interfaces instead.

Revise Inheritance in Java first, then continue with Stacks, Queues and Linked Lists for ISC Class 12.