C # Abstract classes and abstract methods

Source: Internet
Author: User

First of all, in learning abstract methods, we must understand why there are abstract methods in C #, or why abstract methods are needed.

We know that an interface can contain the declaration and properties of a method, but that the method does not contain code, and that the method that implements the interface must contain all the methods and properties in the interface, but now this situation is assumed to require an "interface" that requires the class to implement certain methods and properties, but this "interface" contains some code. Instead of having to implement some methods repeatedly in each derived class, you need an abstract class, not an interface, for this case.

By definition, abstract classes and general classes have similarities and differences, and interfaces have similarities and differences. The following author will be revealed to you.

The same point of the abstract class and the general class: You can inherit other classes or interfaces, you can derive subclasses, and there are specific methods;

Different points: Abstract classes have abstract methods, the general class is not, abstract classes can not be instantiated, the general class is possible;

Second, abstract class and interface of the same point: can not be instantiated, inheriting the subclass of the abstract class must implement abstract methods in the abstract class, the implementation of the interface subclasses must implement all the methods and properties in the interface.

Different points: Abstract classes In addition to abstract methods, there are specific methods.

Abstract classes differ from generic classes and interfaces in code representations:

1: abstract class Class_abstract_name;

2, General class: Class Class_name;

3, interface: interface interface_name;

4. Abstract method: The public is an abstract void test (), or public is the only method that must use the public modifier.

5, General method: public/private/protected/. void/string/int/. Method_name ();

6. Virtual method in parent class: public virtual void test_1 (), or virtual public void test_1 ();//virtual method must use the public modifier.

7. The virtual method or abstract method that implements the parent class in the subclass: public override void Test_1 (), or override public void test_1 ();

Attention:

Only abstract classes can have abstract methods, but abstract classes can have specific methods. If you put an abstract method in a class, you must identify the class as an abstract class.

The instance code is as follows:

[CSharp]View PlainCopy
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Text;
  5. Using System.Threading.Tasks;
  6. Namespace Testoverride
  7. {
  8. Class Program
  9. {
  10. static void Main (string[] args)
  11. {
  12. Test test = new test ();
  13. Test.test ();
  14. Test.test_1 ();
  15. Test.test_2 (); //Parent class, there are no methods in the subclass, subclasses can still use
  16. Console.readkey ();
  17. }
  18. }
  19. abstract class Method_abstract
  20. {
  21. abstract public void Test ();
  22. virtual public void Test_1 () {
  23. Console.WriteLine ("This is theTest 2.");
  24. }
  25. public void Test_2 () {
  26. Console.WriteLine ("This is theTest 3.");
  27. }
  28. }
  29. Interface Interface
  30. {
  31. //The methods and properties in the interface are public, so you do not have to enter public
  32. void Test ();
  33. string Test_string { get; set;} //This is an attribute in the interface
  34. }
  35. Class Test:method_abstract,interface//Abstract classes can certainly be used as a parent class, except for the above differences, there is no difference between abstract and generic classes
  36. {
  37. public override void Test ()//Implement abstract method of abstract class
  38. {
  39. Console.WriteLine ("This is theTest 1.");
  40. }
  41. override public void Test_1 () {//implements the virtual method in the parent class
  42. Console.WriteLine ("This is theTest 1.");
  43. }
  44. public string test_string { get; set;}  //If all properties in the interface are not implemented in the subclass, the code will error.
  45. }
  46. }

The results of the code run are as follows:

Abstract classes and abstract methods are explained to this end!

C # Abstract classes and abstract methods

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.