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?
The answer is 'Yes'. We can have two or more static methods with the same name, but differences in input parameters.
Constructor is a non-static method having the same name as its class - Core Java.
A non-static class can have both static and non-static members so that the static members apply to the class, whereas the non-static members apply to the instances of that class. Show activity on this post.
We currently do not allow two members declared in the same class to have the same name, even if one is static and the other is an instance member.
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.
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.**
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With