I read the articles, watch the video and notice the following construction:

Animal animal = new Dog(); 

Where the author said: Object of class Dog of type Animal.

Let's break it down:

  1. Animal is a class
  2. animal is the name of the object
  3. new - the keyword that is responsible for creating the object
  4. Dog () - constructor

I do not understand why he said:

Object of class Dog of type Animal.

I'm used to this:

 Animal animal = new Animal(); 

Why do we create an object of the Dog class here?

 Animal animal = new Dog(); 

If we have a class and the name Animal Animal, and new Dog() is the creation of a constructor, or we have new Dog() that means the creation of an object, then why is the creation of an object if we have Dog () a constructor? Why is an object created?

Why else say the class is the object?

  • Probably the author of the article tried to teach the reader polymorphism with this example (in contrast to the author answers that push for inheritance). If you do not understand this author, then look for another, more distinct. Then it becomes clear that he wanted to show. - Sergey
  • Yes, about polymorphism, but looked at another where I saw the redefinition of methods and everything fell into place. But this design does not give rest - Petrovchenko Ivan
  • For polymorphism to work, it is not necessary to override methods This is a slightly different and parallel override. I think so. - Sergey
  • @Sergey Yes, I'm already tired of this polymorphism, there are different implementations everywhere, each language has its own features, on udemy.com in normal courses only redefinition of methods is shown, I learned at the interview that overload does not apply to polymorphism in the framework of Java YaB, when on one site It is written that this is polymorphism -_- - Petrovchenko Ivan

2 answers 2

Why do we create an object of the Dog class here?

Because the Dog class most likely inherits the Animal class.

then why is this the creation of an object if we have a Dog () is a constructor?

Dog is not a constructor, but a class. But when creating an animal , a constructor from the class Dog .

Why else to say the class is the object?

Because classes describe objects.


Animal animal = new Dog(); - creates a reference to the Dog class of type Animal . Ie, just like a primitive variable is an int type, an animal an Animal type (but it still refers to Dog ).

What is it for? I will give an example. You have three classes: Animal , Dog , Hourse .

 class Animal{ int speed; boolean live; String name; public void speedUp() {speed++;} } class Dog() extends Animal{ public Dog(int speed, boolean live, String name) { super.speed = speed; super.name = name; super.live = live; } } class Hourse() { public Hourse(int speed, boolean live, String name) { super.speed = speed; super.name = name; super.live = live; } } 

You do not need to rewrite all the variables and methods that Animal in the class Hourse and Dog .

Suppose you have a list of animals (let it be called a pen, from English - a pen). And you need to put in the corral of all animals, and dogs, and cows, and horses. Then you simply declare the list. ArrayList<Animal> pen = new ArrayList<>(); , and you can put all animals there (in our case animal , although it would be more correct to call the variable Dog . But not one dog object, but hourse1 , hourse2 , dog10 , their classes inherit the Animal class).

Example with code:

 ArrayList<Animal> pen = new ArrayList<>(); Animal hourse1 = new Hourse(0, true, "1"); Animal hourseBlack = new Hourse(); //не указываю параметры Animal hoursePink = new Hourse(); Animal bigDog = new Dog(); pen.add(hourse1); pen.add(hourseBlack); pen.add(hoursePink); pen.add(bigDog); 

This is only one advantage of using polymorphism. Another, as I already wrote in the article, is reuseability of the code, when you can prescribe the same behavior for the heirs ( Dog , Hourse ) in the same class ( Animal ). So you can do it like this: hourse1.speedUp(); bigDog.speedUp(); .

  • If you do this: Dog eineKleineDog = new Dog(); pen.add(eineKleineDog); Dog eineKleineDog = new Dog(); pen.add(eineKleineDog); doesn't it work? - Sergey
  • Maybe my eineKlineSobaken-and in a simple List can not be placed? Object-> Animal-> Dog - Sergey
  • Resolve the dispute can compile source code. My compiler allows me to do as I wrote in my comments above :) - Sergey
  • @Sergey yes, sorry. You were right. Do you think it would be logical for the compiler to behave as I described? - Anton Sorokin
  • The compiler works exactly as intended. This is a bad example of polymorphism. Well, in fact, it shows polymorphism, but not Animal, but with Dog, and ... of the pen: -o array. Polymorphism in a broader sense than what little OOP lovers usually think. The correct example is as usual to call the function of the same name via the link to Animal. There is nothing new here - Sergey

Animal is a superclass for some (abstract) animals. Dog is its subclass (child). Therefore, we can declare a variable of a more abstract type, creating objects of different subclasses. This is used in polymorphism, when, for example, we want to put many animals (subclasses of the class Animal ) into a list or an array and operate with them, for example, call their fly() method ...

Another example, we have an interface Sort , in which the sort(...) method is specified. And we have several classes that implement this interface (for example, QuickSort , MergeSort etc ...). Then we can create several variables of the Sort type and use them, substituting in the right place depending on the algorithm we need:

 Sort sort; sort = new MergeSort(); sort.sort(...); // используем алгоритм mergeSort sort = new QuickSort(); sort.sort(...); // используем алгоритм quickSort