[go: up one dir, main page]

0% found this document useful (0 votes)
83 views5 pages

Overloading and Overriding in Salesforce

Uploaded by

joharsai369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views5 pages

Overloading and Overriding in Salesforce

Uploaded by

joharsai369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Overloading and Overriding in Salesforce

Overloading and Overriding are both important concepts in Object-Oriented


Programming (OOP), including in Salesforce’s Apex language. They are used to
handle methods in different ways depending on specific requirements. However,
these two concepts differ significantly in their purpose and behavior.

● Method Overloading.
● Method Overriding.

1. Method Overloading:

Overloading refers to the concept of defining multiple methods with the same
name in the same class, but with different parameter lists. This allows a class to
perform a similar operation in different ways depending on the number or types of
arguments passed to the method.

● Key Characteristics of Overloading:

○ The method names are the same.


○ The parameter lists must be different (either in number, type, or
both).
○ It can happen in the same class.
○ The return type does not play a role in overloading (i.e., two methods
cannot be differentiated by return type alone).

N.Veera Raghavamma
Example:

public class Calculator {


public Integer add(Integer a, Integer b) {
return a + b;
}

public Integer add(Integer a, Integer b, Integer c) {


return a + b + c;
}

public Decimal add(Decimal a, Decimal b) {


return a + b;
}
}

//Anonymous Window

Calculator calc = new Calculator();

Integer sum1 = calc.add(10, 20);


Integer sum2 = calc.add(10, 20, 30);
Decimal sum3 = calc.add(10.5, 20.5);

System.debug('Sum of two integers: ' + sum1);


System.debug('Sum of three integers: ' + sum2);
System.debug('Sum of two decimals: ' + sum3);

Output:

Sum of two integers: 30


Sum of three integers: 60
Sum of two decimals: 31.0

N.Veera Raghavamma
2. Method Overriding:

Overriding refers to the concept of redefining a method in a subclass that is


already defined in its parent class. The subclass method overrides the parent
class method, meaning the subclass can provide its own specific implementation
of that method.

● Key Characteristics of Overriding:

○ The method signature (name, parameters, and return type) must be


exactly the same in both the parent class and the child class.
○ It occurs across inheritance (i.e., between a parent class and a
subclass).
○ The @Override annotation is used to explicitly indicate that the
method is overriding a parent class method.
○ The access modifier of the overriding method should not be more
restrictive than that of the parent method (e.g., if the parent method
is public, the overriding method should also be public).

Example:

public class Animal {

public void makeSound() {


System.debug('The animal makes a sound.');

}
}

N.Veera Raghavamma
public class Dog extends Animal {
@Override
public void makeSound() {
System.debug('The dog barks.');
}
}

//Anonymous Window

Animal genericAnimal = new Animal();


genericAnimal.makeSound();

Animal dog = new Dog();


dog.makeSound();

output:

The animal makes a sound.


The dog barks.

Differences Between Overloading and Overriding:

Aspect Overloading Overriding

Definition Multiple methods with Redefining a method


the same name but from the parent class in
different parameter lists the subclass.
in the same class.

N.Veera Raghavamma
Aspect Overloading Overriding

Inheritance Does not require Requires


inheritance(occurs in the inheritance(occurs
same class). across parent and
subclass).

Method Signature The method name must The method


be the same,but the name,parameters ,and
parameters must be return type must be
different. identical.

Return Type Can be different(but cannot Must be the same as the


differentiate methods parent class method.
based only on return type).

Usage Provides flexibility for the Specialize behavior of a


same action with method for a subclass
different inputs.

Access Modifier No restrictions based on The access level cannot


the original method. be more restrictive than
in the parent class.

Key Differences:

● Overloading:

Same method name, different parameters. Happening in the same class.

● Overriding:

Subclass redefines a parent class method. Happening across inheritance.

N.Veera Raghavamma

You might also like