Calling Methods

When you call a method, you invoke it to execute the code it contains. There are different types of methods in Java, and they can be categorized based on how they are called and their purposes.

Types of Methods

  1. Instance Methods: Called on an instance of a class.
  2. Static Methods: Called on the class itself.
  3. Constructors: Special methods used to initialize objects.
  4. Abstract Methods: Declared without an implementation in abstract classes.
  5. Final Methods: Cannot be overridden by subclasses.

Instance Methods

Instance methods are associated with objects. You must create an instance of the class to call these methods.

class MyClass {
    // Instance method
    public void instanceMethod() {
        System.out.println("Instance method called.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass(); // Creating an instance of MyClass
        obj.instanceMethod(); // Calling the instance method
    }
}

Static Methods

Static methods belong to the class rather than any specific instance. You can call static methods without creating an instance of the class.

class MyClass {
    // Static method
    public static void staticMethod() {
        System.out.println("Static method called.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass.staticMethod(); // Calling the static method
    }
}

Constructors

Constructors are special methods used to initialize objects. They have the same name as the class and do not have a return type.

class MyClass {
    // Constructor
    public MyClass() {
        System.out.println("Constructor called.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass(); // Constructor is called when creating an instance
    }
}

Overloaded Methods

Java allows method overloading, which means you can define multiple methods with the same name but different parameter lists.

class MyClass {
    // Overloaded methods
    public void display(int num) {
        System.out.println("Number: " + num);
    }

    public void display(String str) {
        System.out.println("String: " + str);
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.display(10); // Calls the method with int parameter
        obj.display("Hello"); // Calls the method with String parameter
    }
}

Abstract Methods

Abstract methods are declared without an implementation in abstract classes. Subclasses must provide implementations for these methods.

abstract class MyClass {
    // Abstract method
    public abstract void abstractMethod();
}

class SubClass extends MyClass {
    // Implementing the abstract method
    public void abstractMethod() {
        System.out.println("Abstract method implemented.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass obj = new SubClass(); // Creating an instance of the subclass
        obj.abstractMethod(); // Calling the abstract method
    }
}

Final Methods

Final methods cannot be overridden by subclasses.

class MyClass {
    // Final method
    public final void finalMethod() {
        System.out.println("Final method called.");
    }
}

class SubClass extends MyClass {
    // This method cannot override finalMethod
    // public void finalMethod() {
    //     System.out.println("Cannot override final method.");
    // }
}

public class Main {
    public static void main(String[] args) {
        SubClass obj = new SubClass();
        obj.finalMethod(); // Calling the final method
    }
}