Calling parent function via super is calling child class function

Hi everyone,

I am having a doubt in the below code. Basically, I am not sure why super.test call from extended child is calling display of child class (it is printing “Inside extended class“).
Based on my knowledge, such polymorphism works only when we assign parent class handle to child class and call parent class virtual methods.
But here we are not doing parent=child anywhere then why is display function of child class called?

//-------------------------------------------------------------------------
//						www.verificationguide.com
//-------------------------------------------------------------------------

class base_class;

  function void test;
    display();
  endfunction

  virtual function void display;
    $display(“Inside base_class”);
  endfunction

endclass

class extended_class extends base_class;
  function void test;
    super.test();
    super.display();
  endfunction

  function void display;
    $display(“Inside extended_class”);
  endfunction
endclass

module virtual_class;
  initial begin
    base_class     b_c;
    extended_class e_c;

    e_c = new();
    e_c.test();
    // b_c=new();
    //b_c.display();
  end
endmodule
1 Like

First, you need to use the correct terminology. In your example, you have a base class and an extended class. The extended class inherits the properties of the base class, and allows you to add/modify items from the base class.

A parent/child relationship indicates that a parent class contains an instance of a (unique) child class. There are no common properties between these two classes.

In your example, you are creating an instance of your extended class. In your extended class, the test() function is explicitly calling super.test() and super.display(). The call to super.test() simply calls display, which is overridden by the extended class, hence the reason you see “Inside extended_class”. The call to super.display() specifically calls the display function in the base class, which is why you see “Inside base_class”

@cgales I am not clear why display of parent class is not being called (when calling super.test) as the display function is defined in parent class too .
super.test() is calling test of parent, and that in turn calls display(). The display() function is defined in both parent and child. Why is it specifically calling child’s display() instead of parent’s?

Also, if I remove virtual from display(), we get “inside base class“ as the output

Hi,

You’re actually very close to the right understanding of polymorphism. The key concept here is dynamic dispatch caused by the virtual keyword.

Think of it this way: whenever the simulator encounters a virtual method, it decides at runtime which implementation to execute based on the actual object type, not the handle type.

In your code:

  1. You create an object of extended_class.
  2. You call test() from the child.
  3. Inside extended_class::test(), you call super.test().

Now execution enters base_class::test().
Inside that function, it calls: display();

Since display() is declared as virtual, Even though we are inside base_class::test(), the actual object is of type extended_class.

So the overridden version: extended_class::display() gets executed.

This does not require assigning a base class handle to a child object.
Virtual methods always resolve based on the actual constructed object, not how it was referenced.

Hope it’s clear now!

1 Like

Quite interesting . Thanks @Thobiyas for your inputs