Inheritance and Polymorphism

Source: Internet
Author: User
I once talked to a friend about the singleton mode. At that time, the friend spoke about his unique insights on the singleton model: If the singleton class has several sub-classes, you can control several types and only one instance, even more complex controls can be implemented. At that time, everyone talked about it casually. Now, it makes sense to think about it. In general, the construction method of the singleton class is private, but if it is protected, it can both have subclass and ensure that the external Program Instances of the type cannot be generated through the constructor.

In general, inheritance is the relationship between classes. In the object-oriented thinking, interface-oriented programming is an important point. In this interface-oriented programming, it is misunderstood by many people. Literally, all classes must implement an interface. However, in this way, the dogmatism is committed. Here, let's talk about the differences between interfaces and abstract classes. Of course, there are many syntax differences. In terms of design, interfaces define behaviors and abstract classes define attributes. In my personal understanding, interfaces in interface programming are not called interfaces, but established behavior programming. This established behavior, when interacting with other modules and systems, we can call it a protocol. Therefore, for interface-oriented programming, the principle is to program a protocol and a behavior.

Polymorphism. The basis of polymorphism is virtual functions. In Java, all methods are virtual functions by default. Therefore, the method in Java is very "penetrating.
In. net, override and other keywords are added to avoid such high penetration. Next, let's take a look at the so-called penetration. 1 Class Parent {
2 Public Parent () {
3Print ();
4}
5
6 Public   Void Print () {
7System. Out. println ("Parent");
8}
9 }
10
11 Class Child Extends Parent {
12 Public Child () {
13
14}
15
16 Public   Void Print () {
17System. Out. println ("Child");
18}
19 }
20
21 Public   Class Test {
22 Public   Static   Void Main (string [] ARGs) {
23Parent P= NewParent ();
24Child C= NewChild ();
25}
26 }
27

The above sectionCodeThe final output result is: parent child. This is very strange. In New Child (), we first execute the constructor of the parent class, execute print, but execute the print of the subclass. It's a bit strange to think about it.

Founder is just like this. In terms of syntax, inheritance and polymorphism can be combined to meet the needs of many dynamic changes. If reflection is added, changes are basically involved, there is nothing to fear.
(There is no actual content in this article. You are welcome to shoot bricks)

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.