SystemVerilog Datatype: Class (Part 2)

Previous: Inheritance | Next: Data encapsulation

Inheritance and Scope

Note that, Trapezoid is indeed an extension of the class Triangle and every Trapezoid object is a perfectly legal Triangle object. For example, a handle of a Trapezoid object can be assigned to a Triangle type variable.

 
   Trapezoid  tpzd = new;
   Triangle t = tpzd;

This gives rise to an interesting situation. Consider a scenario where both Trapezoid and Triangle contain a property (or a method) of the same name, but of different definitions. A common occurence of this situation is the method new(). Which definition will t in the above code inherit? Is it the definition of the property/method that Trapezoid has or will it be from Triangle?

In such a scenario, SystemVerilog rules dictate that an object will inherit the definition of its own type. So in this example, t will inherit the property/method definition of Triangle (and not of Trapezoid).

Before we finish on the topic of parent and subclasses, one convenient tip for writing code: you can refer to a variable in the parent class using the keyword super. So, from within Trapezoid, it is always possible to read the value of, say, p1.xy.x of Triangle as super.p1.xy.x.

[Next time: Constructors simplify the use of an object handle and helps us to understand how to use multiple of them together. The concept of subclasses is a powerful one. Using these two concepts - particularly the notion of subclasses - next time we will see how SystemVerilog can help you achieve data encapsulation and the tricky business of polymorphism.]

Previous: Inheritance | Next: Data encapsulation

Share/Save/Bookmark



Verification Management
Join Verification Management Group


Shop Amazon - Contract Cell Phones & Service Plans

Book of the Month