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()?
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.
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