Java, the definition of " polymorphism"


I'm learning Java. I realized that I can't properly define polymorphism. I Googled, found a lot of definitions, but they are hard to come by. I made my own dilettante, but I'm not sure that it is correct and complete. Please add and correct it if it is incorrect, because most likely I have not yet fully understood the essence of the question.

Polyformism is an approach in which a variable of the parent type is allowed to store a reference to its child. Accordingly, it is allowed to use the child is where the parent is expected by the signature, and in the case of" substitution", when trying to call a method, the parent will call the child method. Thus, polymorphism allows you to abstract from specific implementations of methods.

The point is to give a definition without examples.

Author: Kromster, 2018-07-02

3 answers

First of all, I would like to note that polymorphism is one of the three main paradigms of OOP(inheritance, encapsulation, and polymorphism). The second is that polymorphism is the basis for all GoF design patterns, i.e. almost all of them are built on it in one way or another.

Unfortunately, without examples, it will be much more difficult for the author of this question to understand everything without examples... Therefore, to begin with, I will present a simple example of what it is all about:

abstract class Figure {

    ...

    abstract void move();

    ...

}

class Cube extends Figure {

    @Override
    public void move(){
        // moving
    }

}

class Line extends Figure {

    @Override
    public void move(){
        // moving
    }

}

enum TypeFigure {
    CUBE,
    LINE
}

...

List<Figure> figures = new LinkedList<>;
while (true) {
    Random random = new Random();
    int typeFigures = { TypeFigure.CUBE, TypeFigure.LINE };
    switch (random.nextInt(typeFigures.length)) {
         case TypeFigure.CUBE:
              Cube cube = new Cube();
              cube.move();
              figures.add(cube);
              break; 
         case TypeFigure.LINE:
              Line line = new Line();
              line.move();
              figures.add(line);
    }
}

Here here is a small fragment of a game of tetris, in which a random figure is generated in an infinite loop and moves. The bottom line here is that we have different types of shapes that move and rotate differently, but we need to store them together so that we can delete them later(not implemented).

This is what polymorphism was used for, we create an abstract class Figure with an abstract move method, and its descendants with which we will work choose how it is move them to implement...

"Thus, polymorphism allows you to abstract from specific implementations of methods." - in other words, polymorphism allows you to make the code less connected, or Grasp the "Indirection" pattern. Let's go to the example.

  class A {

      private B b = new B();

      public void foo() {
         b.bar();
      }       

  } 

  class B {

      public void bar() {
         //
      }   

  }

In this example, the class А depends on the class В, but it would seem that there is nothing wrong with this. Now imagine that you work for a company and develop a large system. And then, at one point, the programmer "Vasya" comes and he says to you: "Listen, give me a class А, I will use it", in response to his question, he will receive an answer in the sense that in order to use the class А, he will also have to take the class В(and the class В may depend on a bunch of classes). Thus, the code can not be reused by other programmers, and you yourself in other projects or similar situations. And now what it should look like.

 class A {

      // Inject with Spring
      private B b;

      public void foo() {
         b.bar();
      }       

  } 

  interface B {

      void bar();

  }

  class BImpl implements B {

      public void bar() {
         //
      }   

  }

And now when the programmer "Vasya" comes to you and says the same thing, it can safely take your A class along with the B interface (implementing it as it needs it), and not pull all the connections from the В class.

 3
Author: VladimirBalun, 2018-07-21 12:30:42

The term polymorphism can be translated as "Many forms". For example: a person can work as a programmer and engineer. This analogy can be made with classes and methods. Example of polymorphism:

class MyClass {

private int value;

MyClass (int value){
    this.value = value;
}
void method (){ }

void method (String s){ }

private void method (int i) { }
}

class MyClassExtends extends MyClass{

private String s; 

MyClassExtends (int i, String s){
    super(i);
    this.s = s;
}

@Override
void method() { }

@Override
void method(String s) { }

 }

It turns out that in this code there is a superclass MyClass and its subclass MyClassExtends. It turns out that a subclass can use the fields of this class if the field is not private.

There are 3 method () methods here. The first one does not take a parameter; the second string and the third int. It is called overloading is a part of OOP that allows one method to have different forms that differ in input parameters and implementations.

The MyClassExtends class overrides the version of the method () method. Overriding is a part of OOP that allows a subclass (or child) to implement its own superclass method.

It turns out that the term polymorphism can be explained as follows:

Polymorphism is the ability to apply methods of the same name with the same or different sets of parameters in a single class or in a group of classes linked by an inheritance relationship.

Https://o7planning.org/ru/10193/inheritance-and-polymorphism-in-java here you can read more about polymorphism.

 0

Polymorphism is a principle of object-oriented programming that ensures the operation of a descendant class method that overlaps the virtual method of the ancestor class when using the interface of the ancestor class variable, if the variable contains an object of the descendant class.

 0
Author: Андрей Туманов, 2018-07-03 10:09:32