Sunday

Can we overload and override static methods in java

Can we overload and override static methods in java

Overloading

Overloading is the one which same method name but the number of arguments and the type of arguments and the order of arguments can be changed.The return Type is same.

Overriding

Overriding is the process by which overriding the method in parent class by its subclass.

For the above question the answer is YES

public final class Test {


public static void far() {
        System.out.println("aaaaaa");
    }
    public static void far(int a) { 
        System.out.println("bbbbbbbb");
    }
    public static void main(String args[])
    { 
        Test.far();
        Test.far(10);
    }
    

}

But for Overriding the answer is NO

we can't override static methods.

No comments:

Post a Comment