Abstract class based on java, java Basics

Source: Internet
Author: User

Abstract class based on java, java Basics

After java inheritance is completed, let's take a look at abstract classes...

As we have said before, after a thing is abstracted into a specific property and method, it is encapsulated as a class.

We threw the Person's name, gender, and sleeping method into the class Person (professional term: encapsulated into the Person class)

No. Although I mean to abstract the attributes and methods of people into classes, I don't know if we abstract them too specifically? No?

As I mentioned earlier, the attributes and methods of people are abstracted and encapsulated into the class. However, do you not think that name and sex are too specific? There is also a way for a person to go to bed (behavior). When it is, you can define the way to go to bed as a horizontal sleep... is this ......

So we can say that the Person class makes the abstract things come into being. we only abstracted the attributes, but now we initialized them. For example, we changed the name to TianYiLuo gender to male...

So we should give a high-level summary of this thing... oh, let me mention it first. in the abstract class. we are only responsible for the abstraction of methods (Actions). We don't care about the attribute .. why .... too troublesome. no

Come .. let's take the human method (behavior) into a high degree of abstraction .... what do people do .. sleeping .. after dinner, we will abstract the two for now... declare first .. I don't just want to eat and sleep... I will dive... hey

The abstract class is no different from the normal class we have seen before. It's just adding an abstract before the real class and method.

This is the end of abstraction .... is it very concise .. the main difference between an abstract class and a common class is that there is no method body .. the method body is the content in braces, and the abstract class cannot create a new object...

After summarizing things into abstract classes, don't think it's okay ....

Don't tell me... after You encapsulate it into a class, you will ignore it... I didn't mean that before .. the stuff in the abstract class is a common method of some things... what is that... intersection, so when we want to use this class, we need to describe its specific method...

So how to describe it... of course, it is inherited...

Here, we use the student class to embody the abstract methods in Person. of course, other classes can also be used, as long as they are related to the Person class, such as worker (worker) teacher (teacher, maybe the example I gave is not very vivid .. let me briefly explain .... imagine that everyone has their own different behaviors and methods... for example, sleeping .. I can sleep sideways, standing and sleeping, lying down and sleeping .. see, the specific process of each method is different .. but still sleeping .. so we can define it as an abstract class.

Of course, it should be noted that the subclass of the abstract class must fully embody (rewrite) the methods of the parent class. Otherwise, this subclass is still an abstract class and cannot create objects.

Therefore, when using abstract classes, be sure to repeat all the methods once.

 

Benefits of using abstract classes:

1. We can make the program more scalable... I will talk about java polymorphism later ..

2. this makes the program more organized. When an abstract class is used, no matter what the subclass is, We can roughly see the function of the subclass through the abstract class. For example, Person is an abstract class, then we can know that its sub-classes are different people. If the Door is an abstract class, its sub-classes are naturally different doors ..

 

Finally, I want to add something that is easily overlooked... about permission modifiers... here I will talk about the permission size sequence in java:

Public> protected> default> private

If the method method1 of the parent class is modified with public, the subclass can only be modified with public when inheriting and re-writing the method. Otherwise, an error is returned.

If no permission modifier is added to java, the default permission will be used, and then the error will occur. The error of overwriting the high permission (public) with the low permission (default)

Be sure to remember that high permissions can cover low permissions... but low permissions cannot cover high permissions...

... The Windows built-in drawing tool is not used .....

 

 

  

 

 

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.