[introduction to Java] Day1 abstract class

The basic part has almost been explained. Today we begin to enter the Java improvement part. This part will be much more complex than the previous content. I hope you can be prepared. You can read the part you can't understand twice. For the part you still don't understand, I must not speak vividly enough. Remember to leave a message to remind me.

All right, there's no time. Get in the car!

This article mainly explains the abstract classes in Java. What is an abstract class? As the name suggests, it is a very "abstract" class. Abstraction is a pronoun for concrete. Just like the boss told you that if you work hard, you will get a raise, but most of the time you just talk but don't do anything, This is very abstract (2333). If you want to be serious, it is a class with abstract methods. What is an abstract method? It is a method decorated with abstract keyword. The abstract method is only declared but not implemented, and can only be inherited and implemented by subclasses. Let's take a little chestnut:

Here, we still use the goods class to declare an abstract commodity class. You can see that there is a method print modified by the abstract keyword, so this method is an abstract method. The goods class with the abstract method also becomes an abstract class, which needs to be modified by the abstract keyword.

Abstract classes are almost the same as ordinary classes, except that abstract classes cannot be instantiated.

You might ask, what are classes that can't be instantiated for? You don't understand. Obviously, abstract classes are born for inheritance. Non abstract classes that inherit abstract classes must implement the abstract methods of the parent class, otherwise they can only be defined as abstract classes. What's the advantage of this? Here we set up a scenario, that is, commodity information display, which needs to output some description information of each commodity, such as title, price and other parameters, some of which are not common to all commodities, such as screen size and battery life. It is obviously not easy to obtain all commodity information first and then print uniformly, So it's better to leave the printing to the subclass, and the parent class only needs to declare an abstract class. This is equivalent to the task assigned by the father to his son, "if you want to inherit my property, you must complete these tasks first".

There are mobile phones, televisions, computers and other goods under the goods class. Then define three more classes and inherit the goods class, so as to realize the unified management of goods.

After the three classes are defined, let's test:

The output is as follows:

This scene seems familiar. Do you remember the chestnut in the previous inheritance and polymorphism? Yes, so the abstract class is basically the same as the ordinary class. The difference in inheritance is that the non Abstract subclass must implement the abstract method of the abstract parent class, which is not required for the ordinary parent class.

Therefore, generally speaking, an abstract class is an abstraction of a certain kind of things. When multiple classes have the same function but different subjects, a parent class can be abstracted upward. For example, chestnuts, mobile phones, computers and televisions all need to print information, and these have some common properties, so a commodity class can be abstracted, It is used to uniformly manage the output information of these commodities, and this abstract class only declares a method, and the specific implementation is covered by various subclasses.

Now let's perfect our chestnuts to make them look less chicken ribs. Define public attributes and methods in abstract classes, such as title and price, and then define their unique attributes and methods in each subclass.

Next, test:

The output is as follows:

You must have a better understanding of abstract classes now.   

Now make a small summary:

1. The abstract method must be in the abstract class.

2. Abstract methods and abstract classes must be modified by the abstract keyword@ H_ 403_ 335@ 3. An abstract class cannot create an object with new because there are abstract methods. The abstract methods are not implemented and cannot be executed@ H_ 403_ 335@ 4. To use the abstract methods in an abstract class, you must copy all the abstract methods from the subclass, and then create a subclass object call@ H_ 403_ 335 @ so far, the abstract class has been explained. Welcome to continue your attention!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>