Abstract classes and abstract methods

Source: Internet
Author: User

 

Abstract class indicates an abstract concept, but it is expected that the derived classes of the base class have common function members and Data
Member. Abstract classes use abstract modifiers, which have the following requirements:
? An abstract class can only be used as the base class of other classes. It cannot be directly instantiated.
? Abstract classes can contain abstract members, although this is not necessary. Abstract members are modified with abstract modifiers.
? Abstract classes cannot be both sealed.
? The base class of an abstract class can also be an abstract class. If the base class of a non-abstract class is an abstract class, the class must be overwritten
To implement all the inherited abstract methods, including the abstract methods in the abstract base class.
Other abstract classes should also include all abstract methods in other abstract classes.

See the following example:

1 abstract class figure // abstract class definition 2 {protected Double X = 0, y = 0; 3 public figure (double A, double B) 4 {x =; 5 y = B; 6} 7 public abstract void area (); // abstract method, no implementation code 8} 9 class square: figure // class square definition 10 {Public Square (double A, double B): Base (A, B) 11 {} 12 public override void area () // new cannot be used. override13 {console must be used. writeline ("rectangular area: {0}", x * Y);} 14} 15 class circle: Figure // class square defines 16 {public circle (double ): base (A, A) 17 {} 18 public override void area () 19 {console. writeline ("garden area: {0}", 3.14 * x * Y);} 20} 21 class class122 {static void main (string [] ARGs) 23 {square S = New Square (20, 30); 24 Circle C = new circle (10); 25 s. area (); 26 C. area (); 27} 28}


The program output result is:
The rectangular area is: 600
The park area is: 314
Abstract class figure provides an abstract Method Area (), which is not implemented. The class square and circle are from the abstract class
In figure, the Inheritance Method Area () is used to calculate the area of the rectangle.
In the Basic Concepts section of the class, define a class person that describes the individual's situation, which only describes the most
So you do not want to generate its object. You can define it as an abstract class.
Note: C ++ programmers are the easiest to make mistakes here. C ++ does not directly declare the abstract class.
As long as a pure virtual function is defined in the class, this class is an abstract class. The concept of pure virtual functions is obscure and intuitive.
It is not easy for people to accept and grasp, So C # abandons this concept.

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.