Hackforge Academy

Category: java

method override and overload

Published on 14 Feb 2026

Explanation


Method Overloading same method name but different parameters in the same class.
We can change Number of parameters Type of parameters Order of parameters

Code:

class Calculator {
  int add(int a, int b) {
        return a + b;
    }
 int add(int a, int b, int c) {
        return a + b + c;
    }

double add(double a, double b){
        return a + b;
    }
}
//next slide

Explanation

Coding

Code:

public class Main {
    public static void main(String[] args) {
        Calculator obj = new Calculator();
   System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
System.out.println(obj.add(5.5, 2.5));
    }
}
}

Explanation

Method Overriding
Run-Time Polymorphism IT means child class provides its own implementation of parent class method.
Rules: 1.Method name must be same 2.Parameters must be same 3.Inheritance required

Code:

class Animal {
    void sound() {
    System.out.println("Animal makes sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

Explanation

Cont..

Code:

public class Main {
    public static void main(String[] args) {
        Animal obj = new Dog();    
        obj.sound();
    }
}

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community