Oop basic -SV - Inheritance

Hello,


class parent_class;
  bit [31:0] addr;
  
  function new ();
  endfunction
  
  
  function display_parent();
    $display("Hello world");
  endfunction
endclass

class child_class extends parent_class;
  bit [31:0] data;
  
  function new ();
   super.new();  
    
  endfunction
  
  
    display_parent();

  
  function display();
    $display("I am lazy");
  endfunction
endclass



module inheritence;
  initial begin
    child_class c=new();

    c.display();
  end
endmodule

Can anyone tell why can’t i call display_parent() function anywhere in class ?
Whether tasks and function can be called inside a function or a task. I know this is basic but i kind of forgot …

Thanks in advance !!

In reply to tejasakulu:

A class is an object containing properties which are a collection of variables and methods (tasks or functions). Method calls must be made from some procedural code. When do you want the display_parent() method to be called?

In reply to dave_59:

Thanks Dave for the response !! I want display_parent to be called just before the display method of child class.

Question : what do you mean by “method calls must be made from some procedural code “ any example ?

Tejas

In reply to tejasakulu:

I want display_parent to be called just before the display method of child class.

module inheritence;
  initial begin // procedural block of code
    child_class c;
    c =new();
    c.display_parent().
    c.display();
  end
endmodule

In reply to dave_59:

Thanks Dave,

When I call display_parent() inside the new of the child I don’t see any issues, can you tell me why it is so ?


class child_class extends parent_class;
  bit [31:0] data;
 
  function new ();
   super.new();  
   display_parent ();
  endfunction
 
  function display();
    $display("I am lazy");
  endfunction
endclass
 

I have seen in UVM we call functions inside build phase for ex:


Class env;

function build_phase ();
super.build_phase ();
build_ep (); // User defined function 
endfunction

endclass

Does this mean function/task call must be made inside a method ?

Thanks

In reply to tejasakulu:

Function can make a call to another function and task can call another task or function.

Coming to inheritance child can access properties & methods of parent, so from the child class methods we can call parent methods.

Thanks & Regards,
Shanthi V A