After the Java inheritance says: Let's learn about abstract classes ...
As we have said before, it is a class to abstract a thing into a specific property and method and encapsulate it.
We throw People's names, genders, and sleeping methods into the class person, (jargon: encapsulation into the persons Class)
Found no, though I was saying that the attributes of the human, the method abstract encapsulated into class ... But don't you see that we're too abstract to be specific?
As I said before, the human attributes, methods are abstracted into the class, but ... Don't you think the name of sex is too specific for sex? and There's just that. people do sleep this way (behavior), and when it is, you directly define its sleeping method as sideways sleeping ... This is not ...
So we can say that this class of person makes our abstraction come out of the way. we were just abstracting the attributes, but now we're initializing them, like changing the name to Tianyiluo sex to male ...
so, we should give this thing a high-level summary ... oh, i'll mention it first. in an abstract class. we are only responsible for the abstraction of the method (behavior), the property of that thing we don't care About. As for why .... Too much Trouble.
To.. Let us take the human method (behavior) to a high degree of abstraction .... What is the method of man? Go to bed.. Eat well, temporarily abstract these two ... DECLARE first: I'm not just going to eat sleep clams ... I can also dive ... Hey
The abstract class is no different from the normal class we saw Before. Just the actual class and method before adding an abstract ... See
This is the end of the abstract .... is not very concise ah. The main difference between abstract classes and ordinary classes is that there is no method body. The method body is the contents of the curly braces, and the abstract class has no way to new objects ...
When things are highly generalized into abstract classes ... Don't think it's going to be okay ... There's more to it ....
Don't Tell me ... You're going to wrap it up like that. I didn't say it before. What is contained in an abstract class is a common method of a certain kind of thing ... What's that? intersection, So when we want to use this class, we need to describe its specific methods ...
So how to describe it .... of course, with inheritance ... Look at the picture
here, we use the student class to materialize the abstract method within the Person. of course, you can also use other classes, as long as the person (human) class related, such as worker (worker) teacher (teacher) can, Perhaps I cite the example is not very image. I'll simply explain .... Imagine that everyone has their own different behaviors, methods ... sleep, for Example. I could sleep sideways, sleep on my side, sleep on my feet, sleep on my bed. look, the exact process of each method is Different. But It's still sleeping. So we can define it as an abstract class.
of course, It is important to note that the subclasses of the abstract class must fully materialize the methods of the parent class (replication). otherwise, This subclass is still an abstract class. and cannot create objects
So when using abstract classes, be careful to make sure that all the methods are replicated Once.
Benefits of using abstract classes:
1. We can make the program more scalable ... This later will be Java polymorphic when I will say:
2. To make the program more clear, when we use the abstract class, no matter what the subclass is, we can basically see the function of subclass by abstract class, for example, person is abstract class, then we can know that its subclasses are different people, if door is abstract class, its subclasses are different door (door).
finally, I'm Here to add something that is easily overlooked .... Question about the permission Modifier: I'll Talk about it here. the size order of permissions in Java is:
Public > Protected > Default > Private
If the method of the parent class is method1 with public, the subclass can only be decorated with public when inheriting and copying the Method. otherwise, i'll give you an Example.
In java, If you do not add a permission modifier, the default permissions are used, and then the error occurs: overriding High-privilege (public) errors with low permissions (default)
Be sure to remember That. High permissions can override low permissions .... But low permissions cannot override high permissions ... I'll use the diagram below to illustrate
.. A sentence of vomit ... Windows comes with drawing tools .....
Abstract class of Java basics