Is method hiding a form of Polymorphism?

后端 未结 5 1846
夕颜
夕颜 2021-01-03 04:06

Polymorphism is the ability to take many forms. Method overriding is runtime polymorphism.

My questions are:

  1. Is there anything like static polymor

相关标签:
5条回答
  • 2021-01-03 04:13

    If we run this test

    class A {
        static void x() {
            System.out.println("A");
        }
    }
    
    class B extends A {
        static void x() {
            System.out.println("B");
        }
    }
    
    class Test {
        public static void main(String[] args) throws Exception {
            A a = new B();
            a.x();
        }
    }
    

    it will print A. If method x() were polymorphic, it would print B.

    0 讨论(0)
    1. Polymorphism could be static and dynamic both. Overloading is static polymorphism while, overriding is dynamic polymorphism. Overloading in simple words means two methods having same method name but takes different input parameters. This called static because, which method to be invoked will be decided at the time of compilation Overriding means a derived class is implementing a method of its super class.
    0 讨论(0)
  • 2021-01-03 04:22

    I believe Method Hiding would technically be considered polymorphic. By definition, a hidden method has the same signature, or form, as one found in its base class. This is just one of its "many forms". Think of it as Overloading... that happens to "override" the exact same signature. This would be static polymorphism.

    0 讨论(0)
  • 2021-01-03 04:26

    Polymorphism

    Static Binding/Early binding/Compile time binding - Method overloading.(in same class)
    Dynamic binding/Runtime binding/Method overriding.(in different classes.)
    

    Polymorphism in java

    It just has two types, Method overloading and Method overriding, as soon as the method overriding turn into Method Hiding, it loses it's polymorphism features.

    refer to below question from stackoverflow.

    1.) Question1

    2.) Question2

    0 讨论(0)
  • 2021-01-03 04:38

    Polymorphism at runtime takes the form of "dynamic dispatch". That is, the actual method that gets called is determined based on the actual instance you are invoking the method on. Obviously, this applies only when you have an instance of a class, so strictly speaking, polymorphism does not apply to hiding of static methods. For further explanation of the difference check here.

    0 讨论(0)
提交回复
热议问题