Mark As Completed Discussion

Overriding and Overloading

When working with Java, it's important to understand the concepts of method overriding and method overloading, which are two fundamental principles of object-oriented programming.

Method Overloading

Method overloading allows you to define multiple methods with the same name but different parameters within the same class. Each method can perform a similar action but with different types or number of parameters. Java determines which method to call based on the arguments passed when the method is invoked.

Here's an example that demonstrates method overloading:

TEXT/X-JAVA
1// Method Overloading
2public class MathUtils {
3
4    public int sum(int a, int b) {
5        return a + b;
6    }
7
8    public int sum(int a, int b, int c) {
9        return a + b + c;
10    }
11
12    public double sum(double a, double b) {
13        return a + b;
14    }
15
16    public int sum(int[] numbers) {
17        int sum = 0;
18        for (int num : numbers) {
19            sum += num;
20        }
21        return sum;
22    }
23}
24
25public class Main {
26
27    public static void main(String[] args) {
28        MathUtils mathUtils = new MathUtils();
29        int sum1 = mathUtils.sum(5, 10);
30        int sum2 = mathUtils.sum(5, 10, 15);
31        double sum3 = mathUtils.sum(2.5, 3.5);
32        int[] arr = {1, 2, 3, 4, 5};
33        int sum4 = mathUtils.sum(arr);
34        
35        System.out.println("Sum1: " + sum1);
36        System.out.println("Sum2: " + sum2);
37        System.out.println("Sum3: " + sum3);
38        System.out.println("Sum4: " + sum4);
39    }
40}

In this example, we have a MathUtils class that contains multiple sum methods. Each method has the same name but different parameters. The first method sum(int a, int b) calculates the sum of two integers, the second method sum(int a, int b, int c) calculates the sum of three integers, the third method sum(double a, double b) calculates the sum of two doubles, and the fourth method sum(int[] numbers) calculates the sum of an array of integers.

Method Overriding

Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This allows you to change the behavior of the method in the subclass without modifying the superclass implementation. To override a method, you use the @Override annotation.

Here's an example that demonstrates method overriding:

TEXT/X-JAVA
1// Method Overriding
2public class Animal {
3
4    public void makeSound() {
5        System.out.println("Animal is making a sound");
6    }
7}
8
9public class Dog extends Animal {
10
11    @Override
12    public void makeSound() {
13        System.out.println("Dog is barking");
14    }
15}
16
17public class Cat extends Animal {
18
19    @Override
20    public void makeSound() {
21        System.out.println("Cat is meowing");
22    }
23}
24
25public class Main {
26    
27    public static void main(String[] args) {
28        Animal animal1 = new Animal();
29        Animal animal2 = new Dog();
30        Animal animal3 = new Cat();
31        
32        animal1.makeSound();
33        animal2.makeSound();
34        animal3.makeSound();
35    }
36}

In this example, we have an Animal class with a makeSound method that prints "Animal is making a sound". We also have subclasses Dog and Cat that override the makeSound method with their own implementations. When we create instances of Animal, Dog, and Cat classes and call the makeSound method, Java polymorphism allows the appropriate method to be called based on the actual type of the object. The output of the program will be:

SNIPPET
1Animal is making a sound
2Dog is barking
3Cat is meowing

Method overriding is useful when you want to provide different behavior for a method in different subclasses while maintaining a consistent interface in the superclass.

Understanding method overriding and overloading is essential for writing flexible and maintainable code in Java. By utilizing these concepts, you can create more efficient programs and improve code organization and readability.

JAVA
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
JAVA
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment