Function new() overriding ? why should we call super.new() function in derived class constructor?

class A;
int x;
function new(int a);
x=a;
endfunction
endclass

class B extends A;
int p;
function new(int a);
super.new(400);
p=a;
endfunction
endclass

program new_fun ;

A a_o;
B b_o;

initial
begin
a_o = new(1);
b_o = new(100);
end
endprogram

i think while creating object for base class( i.e a_o = new(1) ) , we should not call super.new()function. in above code , i suppose to comment super.new() function in derived class constructor , then tool showing error message ?

please any one clarify it .

Please have a look at the following link, which explains the above stuff very clearly in a step-by-step manner …

Verification Course Blog | VeriLog Courses | CVC

In reply to RAJESHWAR:

hi rajeshwar ,

t i am clear about that concept please see my question again explain it

In your example, you are trying to construct two objects - an A object with class member x, and a B object with class members x and p.

When you construct the A object with a_o = new(1), its member x will be set to 1.

When you construct the B object with b_0 = new(100), the constructor first calls the super.new(400) constructor and x will be set to 400. When it returns p will be set to 100.

So at the end of your initial block, you will have two objects: an A object with member x set to 1, and a B object with members x set to 400 and member p set to 100.

If you comment out the call to super.new(400) in B’s constructor, SystemVerilog will insert a call to super.new() with no arguments for you. Since A constructor requires 1 argument, that implicit call to new() is giving you a compiler error (It is always a good idea to show the error message so we don’t have to guess).

To fix the error, you need to do of three things.

  1. put the call to super.new(400) back.
  2. provide a default value for a in A’s constructor
  3. remove the argument to A’s constructor

In reply to dave_59:

Hi Dave_59 , thank you very much.

one more thing .

in this case ( a_o = new(1) ), the derived class constructor is not calling. so super.new() function also won’t call. but why should we keep suer.new() statement inside derived class constructor ?

even the constructor (new() function ) is not virtual , why the base class object creation is looking for suer.new() statement.

In reply to kbkdec15:

I think you do not understand how inheritance works.

a_o = new(1) constructs a class A object without knowledge of any classes that may be derived from it. And the fact that you have constructed a class A object has nothing to do with the construction of a separate class B object. So remove the statement a_0 = new(1) from your example and this discussion.

When you extend a base class and then construct the extended class, SystemVerilog (and any other object-oriented language) requires that the new() function of the base class gets executed first, then the new function of the extended class next. By making the first statement of every extended new() function call super.new(), this is effectively how the base new function executes first. The base class at the bottom, in your example class A, does not need to call super.new() because there is no super class that it was extended from.

In reply to dave_59:

hi dave , thank you very much.

i understood that clearly. we must include super.new() while creating extended class object.

in this example. generally the tool should not show error when super.new() will delete.
please make sure me about this.


following code should not show any error message because i am creating object for class A, class A is not extends by other class. while creating object the line3 will call then it works good.

but it is not happening like that.

error message are
" super class constructor has non-default arguments. arguments ca be specified by calling super.new() explicitly "

" number of actuals and formals does not match in task call "

Line 1: class A;
Line 2: int x;
Line 3: function new(int a);
Line 4: x=a;
Line 5: endfunction
Line 6: endclass

Line 7: class B extends A;
Line 8: int p;
Line 9: function new(int a);

Line10: p=a;
Line11: endfunction
Line12: endclass

Line13: program new_fun ;
Line14: A a_o;
Line15: initial
Line16: begin
Line17: a_o = new(1);
Line18: end
Line19: endprogram

In reply to kbkdec15:

But you have extended class A in your definition of class B. It does not matter that you have not tried to construct class B. You have written an illegal constructor for class B. The class definition is illegal, and the compiler wont except it. It does not matter even if both class A and B are never constructed, the compiler will reject it as it parses it. The following code by itself will not compile

class A;
   function new(int a);
   endfunction 
endclass
class B extends A;
endclass

The compiler will implicitly insert this constructor into class B;

function new; super.new(); endfunction

and that call to super.new generates an error because it has no argument required by class A’s new.

In reply to dave_59:

Hi dave thank you . i have got it clearly :) :) :) .

In reply to kbkdec15:

Hi Dave,

Here in one of the post , you have mentioned that “Any OOPs langayage or SV requires that the new() function of the base class gets executed first then the new function of the extended class”. Why it is must to do this for SV or any oops lanaguage.

If new method of derived/extended class doesn’t have any dependency on any properly of base class why it is must ?

In reply to jthakkar:

But someone else could create class C extends B, and class C might have dependencies on A. If class B’s constructor doesn’t call super.new() the construction will not work.

Plus there are implementation dependencies in any base class that are not immediately obvious. Class A constructor will allocate memory for its members (x). Class B’s constructor will allocate memory for its members (p) and assume an offset in memory based knowing how much memory class A was going to allocate.

In reply to dave_59:

Hi Jthakkar ,

whenever we extends class , the derived class object can access the base class variable and methods. so memory allocation must be done for base class variables and methods. this can be happen by calling super.new() function.

Hi Dave

I have one clarification. If I extend a class, is it necessary to call super.new() exclusively in the derived class?

In reply to deepthipatawardhan:
Yes.But if there is no input in the new function of base class,super.new() can be omited.It will be excuted by default.Ps:No instruction can be excuted before super.new().That’s illegal.