My questions are:
Why method hiding is also an example of compile time polymorphism while it is not overridden? Related code:
class A{ public static void print(){ System.out.println("class A"); } } public class B extends A{ public static void print(){ System.out.println("class B"); } public static void main(String[] args){ A a = new B(); a.print(); //class A B b = new B(); b.print(); //class B } }If Method hiding is also an example of compile time polymorphism then why variable hiding not an example of compile time polymorphism while variable hiding like method hiding are not overridden and polymorphic?
Sorry my english is pretty bad.
Your code won't compile as-is. Here's an alternate version:
B.java:
Output:
You'll notice:
Now let's try a different example, without "static":
Output:
This is an example of runtime polymorphism.