Method Overriding

In reply to bachan21:
To override the methods the following rules has to be followed

  1. The method prototype should be same in both base class & extended class.
  2. The method should be declared as virtual in the base class
  3. The base class handle should point to extended class object.

class base;

virtual function void display();
$display("I AM IN BASE CLASS");
endfunction

endclass

class child extends base;

virtual function void display();
$display("I AM IN CHILD CLASS");
endfunction

endclass

base b_h;
child c_h;

initial begin
c_h=new();
b_h =c_h;
b_h.display;//o/p=I AM IN CHILD CLASS
end