Why do I get a compile-time error when calling a subclass method using superclass reference?
<p>I know polymorphism happens in the case of method overriding.
But I am a little confused about the below.</p>

<pre><code>class A {
    public void hi() {
        System.out.println("A "+this.getClass().getName()); 
    }
}

class B extends A {
    public void bye() {
        System.out.println("B "+this.getClass().getName());
    }
}

class Ideone {
    public static void main (String[] args) throws java.lang.Exception {
        A a = new B();
        a.hi();
        a.bye();
    }
}
</code></pre>

<p>Output:</p>

<pre><code>Main.java:35: error: cannot find symbol
        a.bye();
         ^
  symbol:   method bye()
  location: variable a of type A
1 error
</code></pre>

<p>Why does this give a compile time error?</p>

<p>In <code>a = new B()</code>, the <code>B</code> class object is created at runtime, so <code>a</code>'s a reference variable pointing to an object of type <code>B</code>.</p>

<p>Now if we call <code>B</code>'s class method <code>bye()</code>,  why it is a compiler time error?</p>