Use of $cast

Hello, i wanted to know how the $cast works if we want to assign a parent class to a child class which is illegal, i want to access the child class variables using parent class, then how it works, plz explain in detail

You first need to understand the difference between a class variable and the class objects it may reference. A base (parent) class variable may contain a handle to a derived (child) class object. You cannot reference derived class members (variables) from a base class variable even when the the base class variable contains a handle to a derived class object. You can only reference members and methods known to the type of the class variable that stores the class handle.

It is always legal to assign a derived class object handle to a base class variable (upcast) because the derived class object inherited everything defined by the base class type. There is nothing you cant reference from the base class variable.

However, when you assign a handle from a base class variable to a derived class variable(downcast), you need to check that the handle refers to an object that has everything defined to reference from the derived class variable.

You may want to read this and search for $cast in SystemVerilog.