Tuesday, March 4, 2014

Overriding vs. Overloading

Overloading is where there is more than one method with the same name, but the methods have different signatures (return type or parameter lists or both).
Overriding is where the superclass's implementation of a method is altered by the subclass's implementation of the method, but the signatures of both methods are the same.
X++ supports overriding, but it does not support overloading.

The methods in a class are inherited by any class that extends it. You can alter the functionality of an inherited method by creating a method in the subclass with the same name and parameters as in the superclass. This is called overriding the method. For example:

// Superclass: Attribute
public class Attribute
{
    int objectVariable;
}
void methodAtt()
{
    //Some statements
}
// Subclass: ColorAttribute
public class ColorAttribute extends Attribute
{
    int addedObjectVariable;
}
void methodAtt()
{
    //Some statements
}
ColorAttribute is a subclass of Attribute and therefore inherits the method methodAttr. However, because ColorAttribute defines a method with the same name and the same number of arguments, the method in the superclass is overridden.

No comments:

Post a Comment