Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static and non static methods with the same name in parent class and implementing interface

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?

like image 517
Arav KT Avatar asked Feb 12 '17 08:02

Arav KT


People also ask

Can we have static method with same name both in parent and child class?

The answer is 'Yes'. We can have two or more static methods with the same name, but differences in input parameters.

Which is a non static method having the same name as its class?

Constructor is a non-static method having the same name as its class - Core Java.

Can a class have both static and non static methods?

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.

Can a static and instance method have same name?

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.


2 Answers

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.

like image 72
Chetan Kinger Avatar answered Oct 28 '22 00:10

Chetan Kinger


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.**
    }
}
like image 3
nits.kk Avatar answered Oct 28 '22 01:10

nits.kk