Inheritance and polymorphism of classes in C #

Source: Internet
Author: User

In order to improve the reusability and scalability of software modules, so as to improve the development efficiency of software, we always hope to make use of our predecessors ' or own previous development results, while we hope to have enough flexibility in our development process and not rigidly adhere to the reusable modules. This leads to two very important features in object-oriented programming language C #: inheritance and polymorphism.

inheritance is an important mechanism to implement code reuse. Using inheritance, you can create an intersection between classes so that newly defined classes inherit the characteristics and capabilities of existing classes, and you can add new attributes or modify existing ones to establish a new hierarchy of classes. The inheritance mechanism of a class is provided in C #, and the base class on which the new class is created is called the base class (base class) or the parent class, and the new class is called an extension class or a derived class or subclass.

Attention:

1, inheritance can be passed. If c derives from B, and b derives from a, then C inherits not only the members declared in B, but also the members of a. The object class acts as the base class for all classes.

2. An extended class can add new members, but it cannot drop the definition of a member that has already been inherited.

3. A derived class can inherit from only one class and multiple inheritance through an interface.

Polymorphic

Another important concept in object-oriented programming is polymorphism. The same operation acts on different objects, can have different interpretations, produce different execution results, this is polymorphism. Polymorphism is implemented by deriving classes that overload the virtual function method in a base class.

In object-oriented system, polymorphism is a very important concept, it allows customers to operate on an object, by the object to complete a series of actions, concrete implementation of which action, how to implement by the system responsible for interpretation.

Implementation through polymorphism can be implemented through inheritance, but also through the abstraction of classes, interfaces to achieve polymorphism.

    in the C # language, defined methods are non-virtual by default (non-virtual) and are not allowed to override these methods, but the method in the base class uses the virtual modifier to become a dummy method. In an extension class, you can override either the virtual method of the base class or not. Pay attention to overloading and overriding. Overloads are: The method name is the same, the argument list is not the same, the return value type can be different, and the overwrite is the distinct implementation of a method repeatedly defined in subclasses to satisfy its own needs, and the override keyword is used to implement the overwrite, and only virtual methods and abstract methods can be overwritten. Overwrite requires the same method name, argument list, and return value type.

Namespace ConsoleApplication1
{
    class program
    {
        static void Main (string[] args)
        {
            Circle tt = new Circle ( );   Inherit
            Console.WriteLine (TT. Getsides ());

            Square dd = new Party ();
            Console.WriteLine (DD. Getsides ());

            Sharp ss = New Circle ();   Polymorphic
            Console.WriteLine (ss. Getsides ());
        }
    Abstract public class Sharp
    {public
        abstract int getsides ();
    }
    public class Circle: Sharp
    {public
        override int Getsides ()
        {
            //throw new notimplementedexception (); C24/>return 1;
        }
    }
    Public class party: Sharp
    {public
        override int Getsides ()
        {
            //throw new notimplementedexception (); C32/>return 2;}}
 

In layman's terms, inheritance is a subclass that inherits the parent class, and polymorphism is a way for the parent class to use subclasses.

First contact with C #, a new language. May be some obscure, some difficult to understand, but hope to be a new posture to face, maintain a good attitude and spirit.

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.