Sunday, June 24, 2018

What are Method Overriding and Method Overloading? What is the difference between the two?

Method overloading is the ability of the functions with same names to be defined multiple times with different set of parameters.

For Example: A function add() can be defined in three formats as shown below. Based on the type and number of arguments passed when the function is called, the correct definition will be picked up.

1) function add (int operand1, int operand2);
2) function add (int operand1, int operand2, int operand3)
3) function add (float operand1, float operand2)

Method overriding is the ability of the inherited class redefining the virtual method of the base class.
Method overriding is supported in most of the object oriented programming languages.
However, unlike C++, method overloading is not supported in SystemVerilog language.
SystemVerilog language only supports method overriding in terms of virtual methods and derived classes. For Example: A base class can define a function called compare() which compares two data members of its class as follows

class BaseClass;
   int a, b;
   virtual bit function compare();
      if(a==b) return 1;
   endfunction
endclass

A derived class can override this definition based on new data members as shown below:

class DerivedClass extends BaseClass;
   int c;
   function compare();
       if((a==b) && (a==c)) return 1;
   endfunction
endclass

No comments:

Post a Comment