JAVA: Why use "abstract classes"? What are the benefits of using "abstract classes"?

I always wonder why we need to refer to abstract classes, aren't general classes enough? Methods defined in general classes can also be overridden by subclasses, so there is no need to define them as abstract.

After reading the article below, I understand a little bit.

In fact, it is not to say that the abstract class is of any use. The general class can indeed satisfy the application, but in reality, it is true that some methods in the parent class do not need to be written, because the method in each subclass will definitely be different, so there is no need for the parent class. write in the class. Of course, you can also write abstract classes as non-abstract classes, but this is not necessary.

 

And write it as an abstract class, so that when others see your code, or when you see other people's code, you will pay attention to the abstract method, and know that this method is implemented in the subclass, so there is a hint.

 

==============

 

Question for you, do you know what a "thing" is? What is an "object"?
"Excuse me, Xiao Wang. Can you help me get that thing?"
In your life, you must have used this word - stuff.
Xiao Wang: "Do you want me to help you get that water glass?"
What you want is an object of the water cup class. And Thing is the parent class of water glasses. Usually stuff classes don't have instance objects, but we sometimes need a reference to a stuff to point to its subclass instance.

Look at the mess in your room, don't put things in a mess in the future, you know?
Another thing, it's an array. The elements in the array are all instances of its subclasses.
---------
The above is only about subclasses and superclasses. And did not explain the role of abstract classes. An abstract class is a class that has one or more abstract methods and must be declared abstract. The characteristic of abstract classes is that instances cannot be created.

These damn abstract classes, don't know what the hell they do. I have to change it. Change the abstract methods in the abstract class to empty implementations. That is to add a method body to the abstract method, but the method body is empty. This time the abstract class has no abstract methods. It can no longer be abstract.

When you try this, you find that nothing has changed from the original code. Everyone is still the same as before, working very well. You may be more convinced this time that abstract classes are useless at all. But don't give up, it should be useful, otherwise, wouldn't the legendary geniuses who created Java become fools?

Next, let's write a small game. Tetris! Let's analyze what class it needs?
I know it's going to be done in a rectangular house. A block appears on the top of the house, and it slowly falls. When it touches the ground or a corpse of another block, it stops falling. Then a new block will appear on the top of the house, and like the previous block, it will slowly fall. I can move and flip it as much as I can before it dies. This can make it play a role when it hits the ground, and if it's good, it can also reduce a few lines. It seems like life, it is working hard for later people.
Of course, we're not really trying to write a game. So we simplify it. I abstracted out two required classes, one for that room, or just a map for it. The other is a block. I found that there are many kinds of cubes, count them, there are 6 kinds in total. They are all made up of four small rectangles. But there are many differences between them, for example: their flipping method is different. Putting that question aside for now, let's go back to the house class.

There is always a block falling on the house, and the house should have an attribute of block. When a block dies, create another block and make it appear on top of the house. When the player flips the method, which square does it flip? Of course, there is only one block in the house that can be flipped, and that is the current block. It is a property of the house. So what type of property is this? There are many different blocks, there are as many as 6, and I need to write 6 classes. There cannot be six types of attributes. Of course a property can only have one type.

We write a block class and use it to derive 6 subclasses. The type of the current block property of the house class is the block type. It can point to any subclass. However, when I call the flip method of the current block, does it's subclasses have it? If you write the flip method into the block class, its subclasses will naturally have them. The flipping methods for these six categories are different. We know the 'field' cube, it has only one state, no matter how you flip it. The long block has two states. One is '-' and the other is '|'. what should we do? We know Java's polymorphism, you can let subclasses override superclass methods. That is to say, this method is defined in the parent class, and the subclass is overriding this method.

So what code do I write in this flip method of the parent class? How many states can it have? Since we can't instantiate an instance of the block class, the code in its flip method doesn't matter. And subclasses must override it. Then you can write no code in the flip method of the parent class, that is, the empty method.

We found that it is impossible for a method class to have an instance, and the content of its flip method can be any code. The subclass must override the flip method of the superclass. At this time, you can write the block class as an abstract class, and its abstract method is the flip method. Of course, you can also write the block class as non-abstract, or write thousands of lines of code in the flip method of the block class. But is this good? Are you sent by Microsoft to say that a lot of things in Java are useless?

When I see that the block class is abstract, I care about its abstract methods. I know its subclasses will definitely override it, and, I'll go and find a reference to the abstract class. It must have the embodiment of polymorphism .

However, if you didn't, I would think there might be somewhere where you instantiate an instance of the block class, but I've looked everywhere and can't find it. At the end I will yell at you, are you here to deceive me, you idiot.

Write abstract classes that are similar to "things". And a class like a water cup need not be abstract. Of course, there are also a few thousand dollars a cup and a few dollars a. There are also subcategories of water cups. For example, the water cups I use are all high-end, mostly disposable paper water cups.

Remember, OO didn't come from Java, OO is in your life. And Java's object-oriented is convenient for you to solve complex problems. That's not to say OO is simple, although OO is complex, Java knows, you know OO because it's all around you.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327000225&siteId=291194637