10

Am not asking about difference between interface and abstract class.

It is working success individually, right?

interface Inter {
    public void fun();
}

abstract class Am {
    public static void fun() {
        System.out.println("Abc");
    }
}

public class Ov extends Am implements Inter {
    public static void main(String[] args) {
        Am.fun();
    }
}

Why is it getting a conflict?

1
  • 3
    What's the question?
    – shmosel
    Feb 12, 2017 at 8:07

2 Answers 2

25

A static and non static method can't have the same signature in the same class. This is because you can access both a static and non static method using a reference and the compiler will not be able to decide whether you mean to call the static method or the non static method.

Consider the following code for example :

Ov ov = new Ov();
ov.fun(); //compiler doesn't know whether to call the static or the non static fun method.

The reason why Java may allow a static method to be called using a reference is to allow developers to change a static method to a non static method seamlessly.

2
  • 6
    This is not 100% correct: you can have same name for member and static methods provided they respect the same rule for overloads, meaning that the signatures should differ at least in the parameter types.
    – ceztko
    Nov 21, 2019 at 16:53
  • 1
    @ceztko Of course. I believe it was implied that we were talking about the same signature and not just the same name; however, edited the answer to make this clear. Apr 26, 2021 at 8:25
3

We have to write our code so that it is syntax wise correct. Also equally important is to understand that our code does not puts any ambiguity for the compiler. In case we have any such ambiguity, the language designers have taken care to not allow the such code to compile.

A class inherits the behaviours from its super class. Static methods can be accessed from simply using class name and also from the instance. Suppose there is method with same name and signature (except for the static keyword), invoking the method on the instance will leave the compiler go for a toss. How will it decide what the programmer intents to do, whcih of the two methods he or she intends to invoke ?. Hence the language designers decided to have this case result in a compile error.

As per

http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8.2

If a class C declares or inherits a static method m, then m is said to hide any method m', where the signature of m is a subsignature (§8.4.2) of the signature of m', in the superclasses and superinterfaces of C that would otherwise be accessible to code in C. It is a compile-time error if a static method hides an instance method.

public class Ov extends Am implements Inter {
    public static void main(String[] args) {
        Ov.fun(); //static method is intended to call, fun is allowed to be invoked from sub class.
        Ov obj = new Ov();

        obj.fun(); //** now this is ambiguity, static method can 
                   //be invoked using an instance, but as there is  
                 //an instance method also hence this line is ambiguous and hence this scenario results in compile time error.**
    }
}
1
  • static method can be invoked using an instance. Nope. Nothing in Java can be invoked using an instance. (Atleast not by the programmer) Feb 12, 2017 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.