Also talk about polymorphism, interfaces, abstract classes

Source: Internet
Author: User

New Java, this part of the knowledge point should be a threshold (anyway I was trapped for a long time), may be because these points will doubt themselves, doubt their IQ, doubt their choice, and then doubt life ... To the point:

1. What is polymorphic:

is a variety of forms (p), an abstract class, due to multiple classes of inheritance and rewriting methods, and the resulting variety of classes, called this abstract class polymorphism. An interface, due to multiple classes implemented and overridden by the method body, produces a variety of classes, called this interface polymorphism.

2. What is abstract:

is to pull out the elephant!

Tall on the explanation: big, wide also. The elephant, the elephant of the heavens and the earth. The phenomenon of universal existence in the universe is drawn out.

Common explanation: to distill common ground.

3. What is abstract method?

  Distill the common denominator of many methods to form a method. But Java found, I wipe! In addition to the method name is the same, the others are different, well, the abstract method is only the method name, even {} are not allowed, enough abstraction.

4. What is abstract class? What's the use of this thing? When do you use it?

Abstract classes are abstract classes (p), which have one or more abstract methods (no concrete method body and {} method, only one method name), then this class is called an abstract class and must be an abstract class.

Small white: For Mao to have an abstract method, did not think well how to write, you want to finish writing not?

Aunt: You know a **!. I don't think so, okay? Just a way to put the name on it is to allow different people to write different implementations of the body, so play a template role, reduce the amount of code.

For example: Male employees, female employees, gey employees, they have to eat, sleep, punch the three methods, and methods are the same, all with the mouth to eat, are lying down to sleep, all with the finger punch, I do not need to write a class, so really tired ... Make a generic template and let them inherit (this is the biggest part of the abstract class).

Small white: But this does not become a common class of inheritance? For Mao to engage abstract class?

Aunt: Didn't you find out that they didn't go to the bathroom the same way? -o-, so the toilet this method I write a name, do not write, do not write concrete method is abstract method, this class is abstract class. So there are:

Eat (with your mouth);

Sleep (lying);

Punch-In (by hand);

Toilet ();

When more than one class, most of the way, a few methods are different, we write an abstract class, refining a common method of writing well, different ways to write a good name, who inherits who rewrite, so reduce the amount of code is also easy to standardize management (this is when using abstract class).

5. What is an interface? What's the use of this thing? When do you use it?

  Interface is the abstraction of abstract class!! Wow, there is no more, yes, the method is really the name, there is no method body.

Because he does not have any method body, also does not have the function of the abstract class template, that for Mao also engaged him? Perhaps his greatest function is to have no effect ...

1. Because he is clean, you can rewrite it in his prescribed method. The role is manifested, the interface is a standardized thing.

For example a silo class needs a "Truemen" object, because the silo needs to truemen very frequently, and often replaced, as if our program needs to replace the database ...

That will be the next definition for truemen, or a number of methods, such as deep (), shallow out (), Circle () three methods, Zhang San think it is truemen, want to docking with the silo, it is necessary to achieve this truemen this interface, and implement all the methods inside, So the silo said Truemen. In-depth () truemen. In-depth () truemen. In-depth () will not error, in order to successfully complete the docking, as for the Dick and Harry Harry they all so take the realization of the well-rounded circle of silos do not care. This is the standardized role of interfaces.

2. A class can implement multiple interfaces, which is more flexible than abstract classes.

  

  

  

Also talk about polymorphism, interfaces, abstract classes

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.