Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ 2018.1.3 shows all the methods from the interfaces, why? [closed]

Lets say we have the following code:

public interface Dvd {
   void startDvd();
}

and another interface:

public interface Tv {
    void startTv();
}

and a class:

public class Logitech implements Dvd, Tv {
    @Override
    public void startDvd() {

    }

    @Override
    public void startTv() {

    }
}

then when you make an instance:

public class Demo {
    public static void main(String[] args) {
        Tv tv = new Logitech();
        tv.  //here when we click the . we expect to see only the startTv() 
             //method, as the type of our reference is Tv
    }
}

however what is shown is:

an image of my intellij code complete

isn't this kind of invalidating the abstraction OOP concept as it will always show me all of the methods from the interfaces that my class implements, and sometimes this can be tons of methods will not hide any complexity for me?

Why do I see both methods and NOT only startTv()?

like image 882
Max Yearwood Avatar asked Dec 20 '25 17:12

Max Yearwood


1 Answers

This only works for local variables, and only when IntelliJ is able to be sure that the type of the variable is always actually a certain concrete type (in this case Logitech).

If we add another class which implements Tv

class Foo implements Tv {
    @Override
    public void startTv() { }
}

and then assign to the variable conditionally:

class Demo
{
    private static int bar()
    {
        return 3;
    }

    public static void main(Tv args) {
        Tv tv = new Logitech();

        if (bar() > 2)
        {
            tv = new Foo();
        }

        tv. //only has startTv method
    }
}

then IntelliJ is no longer able to infer the type, so it won't prompt us with methods from Logitech (or from Foo either).

This is also true for method arguments - IntelliJ does not know the true concrete type, so it won't provide you any additional methods.

like image 170
Michael Avatar answered Dec 23 '25 07:12

Michael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!