Method Overloading and Method Overriding are two important concepts in object-oriented programming that allow developers to create more flexible and reusable code.
Method overloading occurs when multiple methods in the same class have the same name but differ in the number or type of their parameters. This allows the same method to handle different types of inputs.
<?php
class MathOperations {
public function add($a, $b) {
return $a + $b;
}
public function add($a, $b, $c) {
return $a + $b + $c;
}
}
$math = new MathOperations();
echo $math->add(5, 10); // Outputs: 15
echo $math->add(5, 10, 15); // Outputs: 30
?>
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass.
<?php
class Animal {
public function makeSound() {
return "Some sound";
}
}
class Dog extends Animal {
public function makeSound() {
return "Bark";
}
}
$dog = new Dog();
echo $dog->makeSound(); // Outputs: Bark
?>
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?