JAVA - Zero to Hero / Object Oriented Programming (OOP)

Methods in JAVA

๐Ÿ“Ÿ What is a Method?

  • A method is a block of code that performs a specific task.

  • It runs only when called.

โ”Why Use Methods?

  • To reuse code

  • To make the program modular and readable

  • To separate logic into manageable blocks


๐Ÿง  Defining and Calling Methods

๐Ÿ”ธ Syntax:

returnType methodName(parameters) {
    // code
}

๐Ÿ”ธ Example:

public class Main {
    static void greet() {
        System.out.println("Hello!");
    }

    public static void main(String[] args) {
        greet(); // calling the method
    }
}
Hello!

โš™๏ธ Method With Parameters & Return Type

public class Main {
    static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        int result = add(5, 3);
        System.out.println("Sum: " + result);
    }
}
Sum: 8

๐Ÿ› ๏ธ Method Overloading

  • Same method name, different parameters.

  • The compiler decides which one to call based on the arguments.

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

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

public class Main {
    public static void main(String[] args) {
        Calculator c = new Calculator();
        System.out.println(c.add(2, 3));       // 2 parameters
        System.out.println(c.add(1, 4, 5));    // 3 parameters
    }
}
5
10

๐Ÿ” Why use it?

To increase flexibility by allowing methods to handle different numbers of inputs.


๐Ÿ”ง Method Overriding

Method Overriding is when a child class uses the same method name, return type, and parameters as in the parent class, but provides its own code (implementation) for that method.

๐Ÿง  Why Override?

To change or extend the behavior of an inherited method from the parent class.


โš™๏ธ Rules of Method Overriding:

Rule

Description

โœ… Same method name

The method in the child class must have the same name as in the parent class.

โœ… Same parameters

It must have the same parameter list (number and types).

โœ… Inheritance

There must be a parent-child relationship (using extends).

โœ… Access modifier

Cannot reduce the access level (e.g., public cannot be changed to private).

โœ… Return type

Must be the same or a subtype (called covariant return type).

โŒ Cannot override

final, static, or private methods cannot be overridden.

โœ… Use of @Override

Recommended to use @Override annotation for clarity and error checking.

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

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

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog(); // Parent reference, child object
        a.sound();
    }
}
Dog barks

๐Ÿ” Why use it?

To achieve runtime polymorphism (dynamic method binding).

๐Ÿ’ก What Happened Here?

  • Animal has a method called sound().

  • Dog overrides that method with its own version.

  • Even though the object is of type Animal, the Dog's method is executed โ€” this is runtime polymorphism.


๐Ÿ› ๏ธ Why Use Method Overriding?

  • To customize or change the behavior of an inherited method.

  • To implement polymorphism (especially useful in object-oriented design).

  • To create more specific behavior in child classes.