Polymorphism in java

Is method overloading an example of run-time polymorphism or compile-time polymorphism?

+3
source share
4 answers

There is no operator overload in Java.

Method overrides allow polymorphism at runtime; method overloading allows compilation time polymorphism.

Read more about this in the tutorial.

see also

+4
source

- . , . , . :

class A{
    public void doSomething(){
         System.out.println("A method");
    }

    //method overloaded with different arguments-- no polymorphism 
    //or inheritance involved!

    public void doSomething(int i){
         System.out.println("A method with argument");
    }
}

class B extends A{

    //method polymorphically overridden by subclass. 

    public void doSomething(){
         System.out.println("B method");
    }
}

//static type is A, dynamic type is B.
A a = new B();
a.doSomething(); //prints "B method" because polymorphism looks up dynamic type 
                 //for method behavior
+2

- , , (java ), , , , . OO , . .

0

Method overloading is an example of runtime binding. The compiler decides which method should be called only at compile time. So this is compile-time polymorphism, or you can say static binding. But overriding a method is a run-time polymorphism, bcz at runtime, depending on the reference to the object, the method call is allowed.

0
source

Source: https://habr.com/ru/post/1744285/


All Articles