# Why base class can't be assigned to derived class?

**URL:** <https://verificationacademy.com/forums/t/why-base-class-cant-be-assigned-to-derived-class/38150>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, Inheritance, OOPS\
**Created:** [October 1, 2020, 1:34pm UTC](https://verificationacademy.com/forums/t/why-base-class-cant-be-assigned-to-derived-class/38150 "2020-10-01T13:34:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohansrikanthsunkara](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@mohansrikanthsunkara](https://verificationacademy.com/forums/u/mohansrikanthsunkara)\
**Post date:** [October 1, 2020, 1:34pm UTC](https://verificationacademy.com/forums/t/why-base-class-cant-be-assigned-to-derived-class/38150/1 "2020-10-01T13:34:57Z")

</div>

Hi,

Why base class can’t be assigned to derived class?

```verilog
class A;
  bit var1;
endclass

class B extends A;
  //No Variables defined 
endclass

module top;
  A a =new();
  B b =new();
  initial
    b = a; // Compilation error
endmodule

```

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/1084_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [October 2, 2020, 2:37am UTC](https://verificationacademy.com/forums/t/why-base-class-cant-be-assigned-to-derived-class/38150/2 "2020-10-02T02:37:09Z")

</div>

_In reply to [SMS](https://verificationacademy.com/forums/t/why-base-class-cant-be-assigned-to-derived-class/38150):_

```verilog

class A; // Base class
  bit var1;
endclass
 
class B extends A;
  bit b1;
endclass
 
module top;
  A a =new(); // pointer to base 
  B b =new(); // pointer to B, with access to a vars 
  initial begin 
    b = a; // Compilation error
          // if it were legal, then b points to the base class A
    b.b1=1'b1; // There would be no "b1" since the pointer b has no access to 
               // the variables in class b (b pointer is to the base class)     
  end 
endmodule

```

Ben [SystemVerilog.us](http://SystemVerilog.us)

---

<div class="post-metadata">

**Author:** ![mohansrikanthsunkara](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@mohansrikanthsunkara](https://verificationacademy.com/forums/u/mohansrikanthsunkara)\
**Post date:** [October 2, 2020, 5:44am UTC](https://verificationacademy.com/forums/t/why-base-class-cant-be-assigned-to-derived-class/38150/3 "2020-10-02T05:44:35Z")

</div>

_In reply to [ben@SystemVerilog.us](https://verificationacademy.com/forums/t/why-base-class-cant-be-assigned-to-derived-class/38150/2):_

Thanks Ben,

My Query relates to the inheritance concept of OOPs.

If no extra properties defined in derived class, why compiler still complains about legal assignment.

```verilog
class A;
  bit var1;
endclass
 
class B extends A;
  
endclass
 
module top;
  A a =new();
  B b =new();
  initial
    b = a; // Compilation error
endmodule

```

–SMS

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [October 2, 2020, 6:13am UTC](https://verificationacademy.com/forums/t/why-base-class-cant-be-assigned-to-derived-class/38150/4 "2020-10-02T06:13:05Z")

</div>

_In reply to [SMS](https://verificationacademy.com/forums/t/why-base-class-cant-be-assigned-to-derived-class/38150/3):_

Because assignment type compatibility rules are based on name of the type alone, not on the contents of the type. That keeps the rules simple and manageable. It’s no different if you had:

```verilog
class A;
  bit var1;
endclass
class B;
  bit var1;
endclass
module top;
  A a =new();
  B b =new();
  initial
    b = a; // Compilation error
endmodule

```

---

<div class="post-metadata">

**Author:** ![mohansrikanthsunkara](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@mohansrikanthsunkara](https://verificationacademy.com/forums/u/mohansrikanthsunkara)\
**Post date:** [October 2, 2020, 7:57am UTC](https://verificationacademy.com/forums/t/why-base-class-cant-be-assigned-to-derived-class/38150/5 "2020-10-02T07:57:37Z")

</div>

Thanks Dave.

When derived class assigned to base class, hope same rule applicable, then why tool not issuing any error.

```verilog
class A;
  bit var1;
endclass

class B;
  bit var1;
endclass

module top;
  A a =new();
  B b =new();
  initial
    a = b; // No Compilation error
endmodule

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [October 3, 2020, 12:40am UTC](https://verificationacademy.com/forums/t/why-base-class-cant-be-assigned-to-derived-class/38150/6 "2020-10-03T00:40:14Z")

</div>

_In reply to [SMS](https://verificationacademy.com/forums/t/why-base-class-cant-be-assigned-to-derived-class/38150/5):_

Maybe that was a bit _too_ simple. The key point you don’t look at the contents of a class type definition for compatibility.
