Creating a class object in Java

I started to understand polymorphism and found the following example: there is a class Soldier, and General is its heir. The first one has the getHealth() method, the second one has the getSlogan() method.

The principle of creating a class object is not clear. There are the following lines of code:

Soldier warrior1 = new Soldier();
General warrior2 = new Soldier();
Soldier warrior3 = new General();

How the object is created. In the first line, we create an object of the soldier type, right? Then the second one will throw an error, and the third one will create a soldier and lead to a general? Why so and when such a cast is necessary after all it is easier to immediately create a general of the general type.

And the second question is based on the third line, I can't apply warrior3.getSlogan(), only warrior3.getHealth() ? And if the getSlogan() method was in the soldier, would such a call be possible? And this is due to polymorphism?

3 answers

Let's start with the concept of polymorphism. Polyformism is a property of a method in this case, when the same method behaves differently.

Let's take an example from life. There is a class Животное. There is a class Утка that is separated from Животное. There is another class Собака, which also inherits from Животное.

Animal is an abstraction where we can simply define a method Голос(). We will declare it abstract or virtual. Depends on the language. There are many Zhivtons, and each class is Утка and Собака will have their own implementations for the Голос() method.

Now that we can create Утку and Собаку and bring them to a Newtone.

The pseudo code will look like this:

Живтоное animals[2];
animals[0] = new Утка();
animals[1] = new Собака();

for (animal in animals) {
    animal.Голос();
}

That is, when calling the Голос() method, we do not really know what object we are dealing with, but we can call its implementation. This is the power of polymorphism.

Now to the question about the available methods. The type of the variable animal defines the available methods. That is why neither method of the class Утки is not known to the class Животное.

Casting types. This is a separate big topic. We have the ability to cast types moving up or down the inheritance tree. Note that moving down is safer because we know the current type of object and can know exactly who it inherits from. Moving up (from abstraction to concrete) is more dangerous, and it is better to use an explicit type conversion to tell the compiler. It all depends on the language, of course. In your In this case, conversion to the general type is not possible because the general object does not exist in the princep. There is only an object of type Солдат.

I highly recommend that you first understand the concepts of object and type. Then a lot of things in OOP will become more clear.

 9
Author: mr_jumper, 2016-04-06 23:05:59

The first line will create an instance of Soldier and assign it to the variable warrior1

The second line is not even compiled, because an object of type Soldier is created, but is assigned to a variable warrior2 of a more specialized type General.

The third line will create an instance of General and assign it to a variable warrior3 of type Soldier. All right, the general is a soldier in this model.

Why is this so and when is such a cast necessary?

Directly to the in this example, this cast does not make much sense. It only demonstrates that such a cast is possible.

In the larger picture of the world, you can have the kill(Soldier soldier) method somewhere else, and, thanks to inheritance, you can pass both soldiers and generals to it. Internally, the kill method on the soldier argument will only have access to the methods that are declared in the Soldier class. They say that the method kill abstracts from implementation details of a particular a soldier.

I can't apply warrior3.getSlogan(), only warrior3.getHealth()? And if the getSlogan() method was in the soldier, would such a call be possible? And this is due to polymorphism?

That's right. And yes, if the Soldier class had a similar getSlogan() method, it would be possible to call warrior.getSlogan(). Moreover, thanks to polymorphism, it would be called general getSlogan().

 6
Author: Nofate, 2016-04-06 16:33:15

Polymorphism allows you to treat the descendant object as if it were an object of the parent class.

public class A {
    private int var = 1;
    int getVar() {
        return this.var;
    }
}

public class B extends A {
}

And somewhere in the code:

A a = new B();
a.getVar();

And it will work.

BUT! you can't turn it around:

B a = new A();

It will immediately give an error so it will not work!

That is, a pointer from a higher genealogical class can serve as a pointer for all its heirs.

 1
Author: Pavel, 2016-11-11 17:05:03