Deep understanding of OOP (4): Polymorphism and inheritance (abstract class), deep understanding of oop

Source: Internet
Author: User
Tags define abstract

Deep understanding of OOP (4): Polymorphism and inheritance (abstract class), deep understanding of oop

In this article, we will discuss one of the hot topics in OOP: abstract classes. Abstract classes have the same concept in different programming languages, but C # is slightly different. In this article, we will use code to implement abstract classes and parse them one by one.
  • Deep understanding of OOP (1): Polymorphism and inheritance (polymorphism during initial binding and compilation)

  • Deep understanding of OOP (2): Polymorphism and inheritance (inheritance)

  • Deep understanding of OOP (III): Polymorphism and inheritance (dynamic binding and Runtime polymorphism)

  • Deep understanding of OOP (4): Polymorphism and inheritance (abstract class in C)

  • In-depth understanding of OOP (5): access modifier in C # (Public/Private/Protected/Internal/Sealed/Constants/Static and Readonly Fields)

  • Deep understanding of OOP (6): enumeration (practical method)

  • Deep understanding of OOP (7): attributes (practical methods)

  • Deep understanding of OOP (8): indexer (practical method)

  • Deep understanding of OOP (9): Events (in-depth understanding)

Abstract Classes

In Microsoft's MSDN, the abstract class is defined as follows:

Abstract keywords can be used to define an abstract class, requiring its subclass to implement functions and attributes of the abstract class. Abstract classes cannot be instantiated. Abstract classes provide unified definitions for directly sharing data and functions with different sub-classes. Abstract classes can also define abstract functions.

 

Abstract Classes Practice

Add the Console program in Visual Studio and name it"InheritanceAndPolymorphism", Add ClassA. cs, and add the abstract class ClassA.

 

using System;namespace InheritanceAndPolymorphism{    public abstract class ClassA    {    }    /// <summary>    /// Program: used to execute the method.    /// Contains Main method.    /// </summary>    public class Program    {        private static void Main(string[] args)        {            ClassA classA = new ClassA();            Console.ReadKey();        }    }}

Compilation error:

Compile time error: Cannot create an instance of the abstract class or interface 'InheritanceAndPolymorphism.ClassA'

Conclusion: an abstract class cannot be instantiated using the new keyword.

Abstract Class non-Abstract Functions

Add code for some non-Abstract functions to the abstract class ClassA:

/// <summary>    /// Abstract class ClassA    /// </summary>    public abstract class ClassA    {        public int a;        public void XXX()        {                    }    }    /// <summary>    /// Program: used to execute the method.    /// Contains Main method.    /// </summary>    public class Program    {        private static void Main(string[] args)        {            ClassA classA = new ClassA();            Console.ReadKey();        }    }

Compilation, still reports an error. Abstract classes cannot be instantiated by The new Keyword regardless of whether they have abstract or non-Abstract Functions.

Abstract Class as the base Class

We use the abstract class as the base class and add ClassB-to inherit from ClassA.

/// <summary>    /// Abstract class ClassA    /// </summary>    public abstract class ClassA    {        public int a;        public void XXX()        {                    }    }    /// <summary>    /// Derived class.    /// Class derived from abstract class ClassA    /// </summary>    public class ClassB:ClassA    {            }    /// <summary>    /// Program: used to execute the method.    /// Contains Main method.    /// </summary>    public class Program    {        private static void Main(string[] args)        {            ClassB classB = new ClassB();            Console.ReadKey();        }    }

Compilation Result: No error is reported.

Conclusion: A class can inherit fromAbstract class, which can be initialized by the new keyword.

Abstract Class non-Abstract function declaration

Declare the YYY function in ClassA-no function body.

/// <summary>    /// Abstract class ClassA    /// </summary>    public abstract class ClassA    {        public int a;        public void XXX()        {                    }        public void YYY();    }    /// <summary>    /// Derived class.    /// Class derived from abstract class ClassA.    /// </summary>    public class ClassB:ClassA    {            }    /// <summary>    /// Program: used to execute the method.    /// Contains Main method.    /// </summary>    public class Program    {        private static void Main(string[] args)        {            ClassB classB = new ClassB();            Console.ReadKey();        }    }

Compilation, The result reports an error:

Compile time error: 'InheritanceAndPolymorphism.ClassA.YYY()' must declare a body because it is not marked abstract, extern, or partial

The conclusion is that the function body needs to be added to YYY orAbstract modifier.

Abstract Class Abstract function declaration

Add the abstract modifier before the YYY of ClassA.

/// <summary>    /// Abstract class ClassA    /// </summary>    public abstract class ClassA    {        public int a;        public void XXX()        {                    }       abstract public void YYY();    }    /// <summary>    /// Derived class.    /// Class derived from abstract class ClassA.    /// </summary>    public class ClassB:ClassA    {            }    /// <summary>    /// Program: used to execute the method.    /// Contains Main method.    /// </summary>    public class Program    {        private static void Main(string[] args)        {            ClassB classB = new ClassB();            Console.ReadKey();        }    }

Compilation result, error:

Compiler error: 'InheritanceAndPolymorphism.ClassB' does not implement inherited abstract member 'InheritanceAndPolymorphism.ClassA.YYY()'

Conclusion: InThe abstract class declaresAbstract function, but does not implement its content in its subclass ClassB; when the new keyword is used to initialize ClassB, an error is reported ---- The new Keyword cannot be used to initializeAbstract class.

Subclass inheritance implements Abstract Functions

Add the implementation of YYY to the subclass.

/// <summary>    /// Abstract class ClassA    /// </summary>    public abstract class ClassA    {        public int a;        public void XXX()        {                    }       abstract public void YYY();    }    /// <summary>    /// Derived class.    /// Class derived from abstract class ClassA.    /// </summary>    public class ClassB:ClassA    {        public void YYY()        {                     }    }    /// <summary>    /// Program: used to execute the method.    /// Contains Main method.    /// </summary>    public class Program    {        private static void Main(string[] args)        {            ClassB classB = new ClassB();            Console.ReadKey();        }    }

Compilation result, error:

Compile time error: 'InheritanceAndPolymorphism.ClassB' does not implement inherited abstract member 'InheritanceAndPolymorphism.ClassA.YYY()' Compile time warning: 'InheritanceAndPolymorphism.ClassB.YYY()' hides inherited member 'InheritanceAndPolymorphism.ClassA.YYY()'.

Conclusion: To make the subclass inherit the YYY function of the base class, the override keyword must be used before the ClassB can be instantiated using the new keyword.

Abstract Functions of Non-abstract classes

Let's look at the Code:

/// <summary>    /// Abstract class ClassA    /// </summary>    public class ClassA    {        public int a;        public void XXX()        {                    }       abstract public void YYY();    }    /// <summary>    /// Derived class.    /// Class derived from abstract class ClassA.    /// </summary>    public class ClassB:ClassA    {        public override void YYY()        {                     }    }    /// <summary>    /// Program: used to execute the method.    /// Contains Main method.    /// </summary>    public class Program    {        private static void Main(string[] args)        {            ClassB classB = new ClassB();            Console.ReadKey();        }    }

Compilation, The result reports an error:

Compiler error: 'InheritanceAndPolymorphism.ClassA.YYY()' is abstract but it is contained in non-abstract class 'InheritanceAndPolymorphism.ClassA'

Result Analysis: StatementAbstract function, must declare the classabstract。Abstract Functions cannot add static or virtual keywords at the same time.

Abstract basic functions
/// <summary>    /// Abstract class ClassA    /// </summary>    public abstract class ClassA    {        public int a;        public void XXX()        {                    }       abstract public void YYY();    }    /// <summary>    /// Derived class.    /// Class derived from abstract class ClassA.    /// </summary>    public class ClassB:ClassA    {        public override void YYY()        {             base.YYY();        }    }    /// <summary>    /// Program: used to execute the method.    /// Contains Main method.    /// </summary>    public class Program    {        private static void Main(string[] args)        {            ClassB classB = new ClassB();            Console.ReadKey();        }    }

Compilation, The result reports an error:

Compile time error : Cannot call an abstract base member: 'InheritanceAndPolymorphism.ClassA.YYY()'

Result Analysis: The base class abstract function cannot be called in ClassB because it does not exist.

 

The last question is whether the keyword sealed can be added to the abstract class. The result is not acceptable.

Abstract classes cannot contain sealed or static class modifiers.

Conclusion

The following points are used to summarize the conclusions of this article.

  • New cannot be used for instantiation.Abstract class
  • Abstract classes can have subclasses. After their subclasses implement abstract methods, they can be instantiated by new objects.
  • If you declareAbstract function, you must declareAbstract class
  • When override abstracts the base class, the signature of the base class function cannot be modified.
  • Abstract function, cannot add static or virtual keywords at the same time
  • Abstract class cannot be declared as sealed or static class

 

Original article: Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Classes in C #)

Http://www.cnblogs.com/powertoolsteam/p/Diving-in-OOP-Day-Polymorphism-and-Inheritance-All.html.

 

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.