Polymorphism - Overload / Override

I know that this question was made to death on StackOverflow and that there are already many questions on it. I probably read each of them, and yet, this is a doubtful doubt . I think I understand Overloading and Overriding very well. My name is Polymorphism.

For example, the accepted answer to this question explains this with shape.Draw(). I am confused as to how this differs from redefinition (in other cases, I am confused by how it differs from overload).

In addition, polymorphism inherently means a conclusion from an abstract class? (I think that almost all the answers I read on this topic use an abstract class of animals and make a cat and dog meow / bark :)

To summarize , my questions are:

  • What is wrt polymorphism Overload and override?

  • Can someone explain polymorphism without an abstract class - thanks!

  • Overload / redefinition are not subtypes of polymorphism, are they?

Edited to add a third question and change the second question.

+3
source share
3 answers

To answer your questions:

  • The ability to select more specialized methods at runtime depending on the object used to call it.
  • Of course. Polymorphism can occur without the participation of abstract classes.
  • No, overloading / overriding are not types of polymorphism.

, .

// non abstract
class A
{
    public void a()
    {
        System.out.println("Hello from A");
    }
}

class B
   extends A
{
    @Override
    public void a()
    {
        System.out.println("Hello from B");
    }
}

class C
{
    public static void SomeStatic(A a)
    {
         // HERE IS WHERE POLYMORPHISM OCCUR
         a.a();
    }
}

C , SomeStatic B. A, A A. B, B A. , .

- . , , . . , .

, , () . . ( ).

C - :

class Main
{
    public static void main(String[] args)
    {
        A a = new A();
        B b = new B();
        C.SomeStatic(a); // will call A a
        C.SomeStatic(b); // will call B a
        // AND THIS IS POLYMORPHISM
        // C will decide WHICH METHOD TO CALL 
        // only at runtime
    }
}

Poly: . . : . .

, "many" (poly) "forms" (morph) . , , .

+7

- .

- - :

  • -.
  • Overriding .
+1

- , . . netbanking.

:

1) : ( , ). , .

2) : . ( ) .

0

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


All Articles