CallNotStaticallyChecked
DRAFT

Misconception:

Given a class C that does not have a method m(), it is possible to invoke that method m() on an object reference of type C, as long as the subclasses of class C implement that method.

Incorrect

One can invoke a method on a reference of a class that does not have that method

Correct

Correction
Here is what's right.

public class C {
}

public class Sub extends C {
  public void m() { /* implementation */ }
}

public class Test {
  public void test() {
    C c = new Sub();
    c.m(); // this will NOT compile
  }
}

If we want to invoke a method m() on a reference of type C, then type C must declare that method.

Language

Java

Concepts

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.