Abstract classes and interfaces in. Net

Source: Internet
Author: User

Learning Design Patterns When a lot of use of abstract classes and interfaces, used to feel they are a bit similar, but also a bit confused, in the end interface and abstract class what is the difference? Today I tried to get them to unravel the mystery of the veil!

Abstract class: Abstract class is the abstraction of the class, it usually represents an abstract concept, and provides an integrated starting point, when the design of new abstract classes, must be used to inherit, so, in an inheritance relationship formed in the hierarchy, the leaf node should be a concrete class, tree nodes should be abstract class.

Characteristics of abstract Classes

  (1) Abstract methods are declared only, not implementations, and can be seen as virtual methods that do not implement the body.    (2) Abstract classes cannot be instantiated.    (3) Abstract classes can but do not have to have abstract attributes and abstract methods, but once you have an abstract method, you must declare the class as an abstract class.    (4) A specific derived class must override the abstract method of the base class. (5) An abstract derived class can override an abstract method of a base class, or it can be overridden.

Example
   Public abstract class Animal        {              ///Abstract classes Properties            private int age;            private string sex;            public int Age            {                get {return-age;}                set {age = value;}            }            Abstract class contains virtual method public            virtual int getage ()            {                return age;            }            Abstract classes can contain common methods public            void Setage (String sex)            {                this.sex = sex;            }            Abstract classes can also contain abstract methods public            abstract void Run ();        }        public class Cat:animal        {            //cat class inherits abstract class Animal and overrides the Run method public            override void Run ()            {                throw new NotImplementedException ();            }        }

Second, the interface

Definition: An interface is a collection of specific functions that combines implicit public methods and attributes.

Once a class implements an interface, the class can support all the properties and members specified by the interface, which is syntactically the same as declaring an abstract class, but does not allow the execution of any member of the interface.

Features of the interface:

(1) The interface cannot be instantiated.

(2) An interface can contain only method declarations.

(3) The members of an interface include methods, properties, indexers, events, and are fully implemented in a specific class.

(4) The interface cannot contain constants, fields (domains), constructors, destructors, static members.

Example

  Public interface Animal1        {            string name (string name);            void Run ();        }                Class Dog:animal1        {public            void Run ()            {                Console.WriteLine ("50km/h");            }            public string name (string name)            {                return Name;             }        }

Interface vs Abstract class

Same point:

(1) Can be inherited.

(2) cannot be instantiated.

(3) can contain method declarations.

(4) A derived class must implement a method that is not implemented.

Difference

(1) Abstract base classes can define fields, properties, method implementations. An interface can only define properties, indexers, events, and method declarations, and cannot contain fields.

(2) An abstract class is an incomplete class that needs further refinement, while an interface is a code of conduct.

(3) Interfaces can be multiple implementations, and abstract classes can only be inherited by a single.

(4) Abstract classes are more defined in a series of tightly related classes, whereas interfaces are mostly in classes that have a loose relationship but implement a function.

(5) Abstract class is the concept of abstraction from a series of related objects, so it reflects the internal commonality of things; an interface is a functional contract defined to satisfy an external invocation, and therefore reflects the external nature of the thing.

(6) The interface basically does not have any specific characteristics of inheritance, it only promises to be able to invoke the method, the interface can be used to support callbacks, and inheritance does not have this feature.
(7) Abstract members in an abstract class can be partially implemented in a quilt, and the interface must be fully implemented.
(8) The concrete method of the abstract class implementation is virtual by default, but the interface method in the class that implements the interface defaults to non-virtual, and of course you can declare it as virtual.

Third, summary

Fundamentally, the abstraction is from the subclass found in the common things, the generalization of the parent class, and then let the subclass to inherit the parent class, is from the bottom up, and the interface is not at all aware of the existence of subclasses, how to implement the method is not known, just pre-defined a function! The above is my simple understanding of interface and abstract class, welcome friends to treatise.

Abstract classes and interfaces in. Net

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.