Inheritance is a concept in OOP that allows us to extend a class to create another class and have access to all the properties and methods of the original parent class from the handle of a new class object. The idea behind this scheme is to allow developers add in new properties and methods into the new class while still maintaining access to the original class members. This allows us to make modifications without touching the base class at all.


ExtPacket is extended and hence is a child class of Packet. Being a child class, it inherits properties and methods from its parent. If there exists a function with the same name in both the parent and child class, then its invocation will depend on the type of the object handle used to call that function. In the example below, both Packet and ExtPacket have a function called display(). When this function is called by a child class handle, the child class display() function will be executed. If this function is called by a parent class handle, then the parent class display() function will be executed.

class Packet;
   int addr;

   function new (int addr);
      this.addr = addr;

	function display ();
		$display ("[Base] addr=0x%0h", addr);

// A subclass called 'ExtPacket' is derived from the base class 'Packet' using
// 'extends' keyword which makes 'EthPacket' a child of the parent class 'Packet'
// The child class inherits all variables and methods from the parent class
class ExtPacket extends Packet;

	// This is a new variable only available in child class
	int data; 		

   function new (int addr, data); (addr); 	// Calls 'new' method of parent class = data;

	function display ();
		$display ("[Child] addr=0x%0h data=0x%0h", addr, data);

module tb;
	Packet      bc; 	// bc stands for BaseClass
	ExtPacket   sc; 	// sc stands for SubClass
	initial begin
		bc = new (32'hface_cafe);
		bc.display ();
        sc = new (32'hfeed_feed, 32'h1234_5678);	
		sc.display ();

Simulation Log
ncsim> run
[Base] addr=0xfacecafe
[Child] addr=0xfeedfeed data=0x12345678
ncsim: *W,RNQUIE: Simulation is complete.

It becomes a little more tricky when you try to assign a child class instance to a base class handle or vice versa. This is covered in more details under Polymorphism