Accessing base class function using derived class object/handle in systemverilog

HI All,

Is there any way to access base class function using derived class object in systemverilog

Please reply.

Thanks,
Rahul Kumar

You can access everything in a base class not marked local in a derived class object. That is what inheritance is all about.

You can use “super.” or “base_class_name::” to get access to overridden base class methods (or hidden ones). Example:

class base;
   virtual function void f(); 
   endfunction
endclass

class der extends base;
   virtual function void f();
      super.f();
   endfunction
endclass

In reply to dave_59:

Unless you have a method that was extended in the sub-class and you want to access the implementation from the base class through a sub-class instance. This isn’t a valid use model at all and this is also what inheritance is about.

In reply to GVreugdenhil:

HI,

Thanks for reply.

In above example, base class function is called in derived class function. This is not what i need.

I want to access base class function using derived class handle.

ex.

initial begin
der der_h;
der_h.f(); <----------- because of polymorphism function “f” of der class will be called, But i want access base class function “f”, How can i access base class function “f” using “der_h” ?

Thanks,
Rahul

In reply to rahulkumarkhokher@gmail.com:

You need to explain a lot more of what and why you want to do that, because you can’t do what your asking for directly.

If you don’t override a method in a derived class, you can still call that method in a base class.

class base;
   virtual function void f();
     // more code
   endfunction
   virtual function void g();
     // more code
   endfunction
endclass
 
class der extends base;
   virtual function void f(); // overrides base method
      // more code
      super.f();
      // more code
   endfunction
endclass
 initial begin
     der der_h;
     der_h = new;
     der_h.g(); // calls base class method
 end

If you want to be able to call a base class method that is overrided in a derived class object, you can create another method in the derived class that calls the base class method in the derived class.

class base;
   virtual function void f();
     // more code
   endfunction
endclass
 
class der extends base;
   virtual function void f(); // overrides base method
      // more code
      super.f();
      // more code
   endfunction
   function void f_base();
     super.f();
   endfunction
endclass
 initial begin
     der der_h;
     der_h = new;
     der_h.f_base(); // calls base method
 end

In reply to dave_59:

Hi,

My doubt is opposite to thread headline but still makes sense to put here.

Is there any way that we can call child class method from base class handle even if that function is not present in parent class?

class base;

// g function not present in base class

endclass

class der extends base;
   function void g(); // method present in child class only
      // more code
   endfunction
endclass

 initial begin
     base b;
     der der_h;
     der_h = new;
     b = der_h;
     b.g(); // calls but gives compiler error that function is not present in class base
 end

Any way so that above can be done?

Thanks
Ankur Jain

Hi Ankur,

You can do that, For the same you need to definde a method in the base class with virtual keyword.

In reply to Ankur Jain4:

In reply to dave_59:
Hi,
My doubt is opposite to thread headline but still makes sense to put here.
Is there any way that we can call child class method from base class handle even if that function is not present in parent class?

class base;
// g function not present in base class
endclass
class der extends base;
function void g(); // method present in child class only
// more code
endfunction
endclass
initial begin
base b;
der der_h;
der_h = new;
b = der_h;
b.g(); // calls but gives compiler error that function is not present in class base
end

Any way so that above can be done?
Thanks
Ankur Jain

Thanks & Regards,
Shriramvaraprasad B.

In reply to dave_59:

Isnt it Dave in the instance you provided you are calling f_base() using derived class handle, but still the f_base() is calling parent function f() which has been overriden by derived class. So anyhow the f_base is also indirectly calling the derived class function only and not base class.

In reply to Ayush_1:

Because f_base() calls super.f(). The
super.
prefix means call
f()
ignoring any overrides in the current derived class
der
.

I think this question thread is very old and confusing. You should ask a new question starting from the beginning.

In reply to SHRI12326:

With the Parent handle, we can access only the overridden methods of the parent in the child. i.e., the method prototype in the child should be the same as of parent & the method should be virtual in the parent.

In your example, the function g is not the overridden method of the parent, so with parent handle we can not access the function g.

Regards,
Shanthi V A
www.maven-silicon.com