Interface Classes (Multiple Inheritance)

Introduced in SystemVerilog-2012, Interface Classes solve the "Diamond Problem" and enable true Multiple Inheritance in testbenches.

Why do we need them?

SystemVerilog, like Java, only allows a class to extend one parent class (`extends Base`). But what if you want a `Driver` to be both a `Component` AND a `PacketListener`?

Interface Class is the solution. It defines a contract (a set of pure virtual methods) that a class must implement. A class can implement multiple interface classes.

Key Features

  • Cannot contain variables (only pure virtual methods and parameters).
  • Cannot be instantiated.
  • A class usually implements an interface class.

Example: API Enforcement

Imagine you are building a generic Scoreboard that can handle any protocol, as long as the protocol object supports a `compare()` method.

Defining the Contract

// 1. Define the Interface Class
interface class Comparable;
    pure virtual function bit compare(Comparable other);
endclass
// 2. Implement it in your Transaction
class Packet extends uvm_object implements Comparable;
    int data;
    // You MUST implement compare() or this class will be abstract
    virtual function bit compare(Comparable other);
        Packet p;
        if (!$cast(p, other)) return 0; // Type safety check
        return (this.data == p.data);
    endfunction
endclass
                            

Multiple Inheritance Example

Building a class that is both a Serializer and a Printer.


interface class Serializer;
    pure virtual function bits[] pack();
endclass
interface class Printer;
    pure virtual function void print();
endclass
// Implements BOTH
class MyData implements Serializer, Printer;
    int a = 5;
    virtual function bits[] pack();
        return { >> {a} };
    endfunction
    virtual function void print();
        $display("Data: %d", a);
    endfunction
endclass
                            

Common Interview Questions

Q: Can an Interface Class extend another Interface Class?
Yes. interface class B extends A;. This builds a hierarchy of contracts.
Q: Difference between Abstract Class and Interface Class?
An Abstract Class can have variables and actual code implementation. An Interface Class can ONLY have pure virtual method prototypes and typedefs/params. A class can extend only 1 Abstract Class but implement N Interface Classes.