1

My questions are:

  1. 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
        }
    }
    
  2. 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.

  • You are calling the `print()` method like a instance method, but it is defined as a static method. Is that intentionally? You should even get a warning in your IDE. – Progman Sep 24 '22 at 16:58
  • "Yes" as in the `static` keyword is intentionally or "yes" as in you get the warning in your IDE? – Progman Sep 24 '22 at 17:06
  • @Progman ·I get the warning in my IDE. When I wrote the code I didn't notice. – tú nguyệt Sep 24 '22 at 17:09

1 Answers1

1

https://byjus.com/gate/difference-between-compile-time-and-run-time-polymorphism-in-java

Q: What Is Compile-time Polymorphism?

A: Compile-time polymorphism is obtained through method overloading. The term method overloading allows us to have more than one method with the same name. Since this process is executed during compile time, that's why it is known as Compile-Time Polymorphism.

Your code won't compile as-is. Here's an alternate version:

B.java:

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 A();
        a.print();
        B b = new B();
        b.print();
        A c = new B();
        c.print();
        //System.out.println(a.a);
    }
}

Output:

class A
class B
class A

You'll notice:

  • A.print() => "class A" // Expected
  • B.print() => "class B" // Also expected
  • class B "is a" A // We can instantiate "B", but use it as "A"
  • C.print() => "class A" // When we do so ... it BEHAVES as "A"
  • This example illustrates compile time behavior

Now let's try a different example, without "static":

class A2{
    public void print(){
        System.out.println("class A2");
    }
}
public class B2 extends A2{
    public void print(){
        System.out.println("class B2");
    }

    public static void main(String[] args){
        A2 a = new A2();
        a.print();
        B2 b = new B2();
        b.print();
        A2 c = new B2();
        c.print();
        //System.out.println(a.a);
    }
}

Output:

class A2
class B2
class B2

This is an example of runtime polymorphism.

paulsm4
  • 114,292
  • 17
  • 138
  • 190