Posted on: January 18, 2025 Posted by: rahulgite Comments: 0

  • Static methods can be overloaded in Java, meaning methods with the same name but different parameter lists can coexist within the same class or in subclasses.
  • Static methods cannot be overridden, but they can be hidden in subclasses.

Overloading Static Methods

Overloading occurs when two or more methods in the same class have the same name but different parameter lists. Since static methods are resolved at compile time, overloading works as expected.

Example: Overloading Static Methods

public class StaticMethodOverloading {
    // First static method
    public static void display(String message) {
        System.out.println("Message: " + message);
    }

    // Overloaded static method
    public static void display(int number) {
        System.out.println("Number: " + number);
    }

    public static void main(String[] args) {
        // Calling overloaded methods
        StaticMethodOverloading.display("Hello");
        StaticMethodOverloading.display(123);
    }
}

Output:

Message: Hello
Number: 123

Key Points for Overloading:

  1. Compile-Time Resolution: Static methods are resolved at compile time, so overloading is straightforward.
  2. Independent of Objects: Static methods belong to the class and not to any object.

Overriding Static Methods

Overriding refers to defining a method in a subclass that has the same name, return type, and parameters as a method in its superclass. Static methods cannot be overridden because they are not associated with any instance and are resolved at compile time. Instead, they can be hidden.

Example: Hiding Static Methods

class Parent {
    public static void show() {
        System.out.println("Static method in Parent");
    }
}

class Child extends Parent {
    public static void show() {
        System.out.println("Static method in Child");
    }
}

public class Main {
    public static void main(String[] args) {
        Parent.show(); // Output: Static method in Parent
        Child.show();  // Output: Static method in Child
    }
}

Key Points for Hiding Static Methods:

  1. Compile-Time Resolution:
    • The version of the static method that gets called depends on the reference type, not the object.
  2. No Polymorphism:
    • Unlike instance methods, static methods do not exhibit runtime polymorphism.

Overriding Instance Methods (for Comparison)

Instance methods in Java can be overridden to provide specific implementations in a subclass. Overriding requires the following:

  1. The method must have the same name, return type, and parameters as in the parent class.
  2. The method cannot have a stricter access modifier.
  3. The @Override annotation can be used for clarity but is not mandatory.

Example: Overriding Instance Methods

class Parent {
    public void display() {
        System.out.println("Instance method in Parent");
    }
}

class Child extends Parent {
    @Override
    public void display() {
        System.out.println("Instance method in Child");
    }
}

public class Main {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.display(); // Output: Instance method in Child
    }
}

Key Points for Overriding Instance Methods:

  1. Runtime Polymorphism: The method that gets executed is determined by the object type, not the reference type.
  2. Dynamic Binding: Instance methods are bound at runtime, enabling polymorphism.

Comparison Table: Overloading vs Overriding

FeatureOverloadingOverriding
Method ResolutionCompile timeRuntime
PolymorphismNot supportedSupported
Associated with Instance?NoYes
Parameter ListMust be differentMust be the same
Access ModifierNo restrictionsCannot reduce visibility
Static MethodsSupportedNot supported (only hidden)

Visibility Order:

  • public > protected > default > private

Conclusion:

  • Static methods can be overloaded, providing flexibility in method design while maintaining compile-time resolution.
  • Static methods cannot be overridden but can be hidden in subclasses.
  • Instance methods support overriding, enabling runtime polymorphism and dynamic behavior.

Understanding these concepts is essential for writing clean, efficient, and polymorphic code in Java.

Leave a Comment