Regarding Method Overriding / Polymorphism SystemVerilog

This works !

I probably see what is wrong. But considering that only a child_handle calls a new() and memory is created only for the child_handle which is then passed onto parent_handle, how is it that there are different parent_handle.a and child_handle.a variables even though I didnt create a parent_handle object?

In reply to Anand Kumar C.R:

Your use of the term “parent” and “child” for classes with inheritance is misleading. When you extend a class , then construct it, the two classes become one. Better terms are “base” and derived" classes. When you construct a “derived” class, you are constructing everything in the base as well.

Yes but isn’t the value of ‘a’ mutually exclusive ? I don’t understand why 2 a’s are created when I have constructed only one object which happens to be the derived class. And that value in the derived class overrides inherited value as per the LRM.

When you say “2 classes become one” shouldn’t the value in the derived class override the value inherited from base class ?

Yes but isn’t the value of ‘a’ mutually exclusive ? I don’t understand why 2 a’s are created when I have constructed only one object which happens to be the derived class. And that value in the derived class overrides inherited value as per the LRM.

When you say “2 classes become one” shouldn’t the value in the derived class override the value inherited from base class ?

In reply to Anand Kumar C.R:

Constraint blocks are virtual, meaning when the constraint block in derived class has the same name as parent class, it would get override. I have modified the code, which works for your requirement !!

module polym_cons();
class parent;
rand int unsigned a;
constraint c1{
(a < 10);
}
endclass

    class child extends parent;
      constraint c1{
        (a > 10);
      }
    endclass

    parent parent_handle;
    child child_handle = new();

    initial
    begin
      parent_handle = child_handle;
      parent_handle.randomize();
      $display("a = %d",parent_handle.a);
    end

endmodule

In reply to sridar:

Thanks Sridar

In reply to dave_59:

Hi,

I have question on this,

class A;
int a=10;
function void disp();
$display("a=%d",a);
endfunction
endclass

class B extends class A;
int a=20;
endclass

program main();
A a;
B b;
b =new();
b.disp();               //diplays a=10
$display("b.a=%d",b.a); // displays a=20
endprogram

My questions is, now b has two a’s and a disp() function. How does simulator know which value of a to display when I call b.disp() ?? what is the OOP concept governing this situation?

2nd question: How can I access a of base class with derived class handle in program block?

In reply to Anand Kumar C.R:

Hi,
When your creating an object of the derived class the simulator adds super.new() constructor and allocates memory for the base class as the derived class is inherited.Hence there will be base class ‘a’ and derived class ‘a’ being created(name hiding).

As per the LRM only virtual methods can be overriden.

can a child class1 can be assigned to child class2 or can we use dymanic casting here

Thanks

In reply to shobhana.swarnkar18:

If you meant to say with a clear example:

class A;
  int member1;
endclass
class A1 extends A;
  int member2;
endclass
class A2 extends A;
  int member3;
endclass

A1 a1_h = new();
A2 a2_h;

// this is not legal
a2_h = a1_h;

There no cast that would make it legal. a2_h.member3 does not exit in the constructed object.

In reply to dave_59:

Thanks
Dave

In reply to Chandrashekhar Goudar:
Sorry about hat I have fixed the code.

class A;
	int a=10;
  
	 virtual function void disp();
       $display("Base a=%d",a);
	endfunction
endclass
 
class B extends A;
	int a=20;
     
  function void disp();
    $display("Derived a=%d",a);
	endfunction
endclass
 
module tb();
  A a=new;
  B b;
  
	
  initial begin
    $cast(b,a);
    
    b.disp();               //
	$display("b.a=%d",b.a); // 
    a.disp();               //
    $display("a.a=%d",a.a); // 
           
    
  end
    
endmodule

I have a compilation error when I do a $cast(b,a) without doing “a=b”. Why is that? Then what is the point having $cast?
Error-[DCF] Dynamic cast failed
testbench.sv, 25
Casting of source class type ‘A’ to destination class type ‘B’ failed due to
type mismatch.
Please ensure matching types for dynamic cast

In reply to ziggy:

You code comment say you have a run-time error, but your text says you have a compilation error. Also, you seem to be missing an
initial begin/end
block. Please correct your post because it really matters to the question you are trying to ask.

In reply to dave_59:

I have fixed my code. So when we do $cast(b,a), it should create a new object of ‘b’ and copy the contents of ‘a’ to ‘b’. Is that right?

In reply to ziggy:

$cast does not create or copy objects; it tries to copy a handle from one class variable to another. Please see my course on SystemVerilog OOP, especially the second session.

In reply to dave_59:

So in the above code I have posted, Class A’s handle ‘a’ is created by ‘new’. Then I do a $cast(b,a). Why is there an error ‘type mismatch’?

But the following code works and why is that?


class A;
   int a=10;
   virtual function void disp();
     $display("Base a=%d",a);
   endfunction
endclass
 
class B extends A;
  int a=20; 
  function void disp();
    $display("Derived a=%d",a);
  endfunction
endclass
 
module tb();
  A a;
  B b=new;

  initial begin
    b=a;
    $cast(b,a);
 
    b.disp();               //
    $display("b.a=%d",b.a); // 
    a.disp();               //
    $display("a.a=%d",a.a); //  
  end
 
endmodule

In reply to ziggy:

The code you just posted does not work. Did you simulate it?

b=a; // should give you a compile error

b.disp(); // should be a run time fatal because b has a null handle.

In reply to dave_59:

Sorry it was a typo
Its “a=b” and not “b=a”

In reply to ziggy:

Have you looked at my course yet?

In reply to dave_59:

Hi Dave,
Your sv-class-OOP courses are excellent !! Heavily loaded with important information. The way you described all of them seamlessly is awesome!