I wrote the game, showed the teacher, he advised me to combine all classes like "enemies" (all enemies) into one abstract one.

Explain why this is necessary, if everything is so good?

Closed due to the fact that off-topic participants are Alexey Shimansky , user194374, Alex , Denis , Denis Bubnov 15 Dec '16 at 7:39 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Alexey Shimansky, Alex, Denis, Denis Bubnov
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Everything related to the question should be in the question, not links to third-party resources .......... In general, questions asking for help with debugging (“why does this code not work?”) Should include the desired behavior, a specific problem or error and the minimum code for playing it right in the question. Questions without an explicit description of the problem are useless for other visitors. See How to create a minimal, self-sufficient and reproducible example - Alexey Shimansky

2 answers 2

To reduce code duplication and its total size

Apparently, the teacher spotted a lot of identical fragments in your code and suggested putting the repeating parts in a separate place. An abstract class is a fairly typical way to do this in OOP languages. Not universal, but more or less everywhere.

In programming, this desire is known as DRY (Don't Repeat Yourself, Do Not Repeat). This achieves several pleasant moments:

  • The amount of code becomes less, the chances of making mistakes in it are less.
  • Making changes to this repeating code snippet, if necessary, is likely everywhere. And it's easier to do it in one place than in all classes separately. Again, the likelihood of making mistakes is reduced.

But do it carefully.

It’s easy to get used to the fact that "putting duplicate code into a class" is always a good idea.

But it is not. And with such an approach it is easy to get stuck in a situation where in any one case the implementation of an abstract class just doesn’t suit you, since the "common part" put into it is slightly different. Well, if you can override desired method (although not, not very well, see below), but the situation explodes if you are forced to make another, very similar class.

And then, following the principle of "making common parts in a separate class," you get another level of hierarchy and the code becomes much more complicated than necessary. The problem seems to be solved, but the code starts to resemble a dense dark forest.

Where is the mistake? In abstraction. Sometimes it is necessary to endure not all common parts, a little duplication can be left if this is seen as a specific part for a specific heir, and heirs without it are possible.

"duplication is far cheaper than the wrong abstraction"

"it is much easier to live with duplication than with abstraction curve"

  • It is great that the answer indicates not only advantages, but also disadvantages of the approach. - Nick Volynkin ♦

It is necessary for your code to comply with the principles of OOP.

Why do I need OOP? - the question is very voluminous, you can read about it, for example, here .

Concerning an abstract class, an irrelevant example:

There are two classes: Cat and Dog , each of which has a field for the name of the animal and a certain method makeNoise() (you have it now):

 public class Cat { private String mName; public Cat(String name) { mName = name; } public void makeNoise() { System.out.println("Meow!"); } } 

 public class Dog { private String mName; public Dog(String name) { mName = name; } public void makeNoise() { System.out.println("Woof!"); } } 

You can define the abstract class Animal and its subclasses Cat and Dog (this is what they suggest you do):

 public abstract class Animal { private String mName; public Animal(String name) { mName = name; } public abstract void makeNoise(); } 

 public class Cat extends Animal { public Cat(String name) { super(name); } public void makeNoise() { System.out.println("Meow!"); } } 

 public class Dog extends Animal { public Dog(String name) { super(name); } public void makeNoise() { System.out.println("Woof!"); } } 

The abstract class defines some structure of its subclasses: both the cat and the dog can make sounds (the abstract method makeNoise() defined in the superclass, but they make different sounds (each of the subclasses uses its own implementation of the makeNoise() method).

  • Do you understand that you are a freelancer now? Just do not ask for payment. Because you answer questions that should be closed, at least until they are edited in accordance with the rules of the resource. You encourage such users to continue to ask questions that do not comply with the rules - Alexey Shimansky
  • one
    @ Alexey Shimansky, No, I do not understand. In my understanding, I do not act as a freelancer (it's obvious, damn it). And the reason why the vehicle did not place the code in question is obviously the fact that there is quite a lot of this code. - post_zeew Nov.
  • one
    This is the problem of the vehicle. He should provide a minimal example. Describe the essence of the problem. If he has 1000 classes and 10,000 lines of code, he has to look and pull out what is needed ..... And the essence of the resource is in accumulating experience and a knowledge base for all, for many. That is, the question should be from search engines and people with similar problems come and see how to solve this problem. And this is clearly not what is written in the question. Obviously the answer will not help anyone except the vehicle. Therefore, you can also freely help via Skype - the meaning will not change. And so only procreate the garbage - Alexey Shimansky
  • @ Alexey Shimansky I agree that the question could be more complete. But the answer fully gives an idea of ​​the principle by which an abstract parent class can be distinguished from several similar classes. - Nick Volynkin ♦
  • post_zeew thanks to my identity with abstracts was not very))) but now it has become clearer) why write to everyone if you can set in one))))))))) - elik