Differences between interfaces and abstract classes-go

Source: Internet
Author: User

 

I. abstract class:
Abstract classes are special classes, but they cannot be instantiated. In addition, they have other characteristics of the class. It is important that abstract classes can include abstract methods, which are not supported by common classes. Abstract METHODS can only be declared in abstract classes and do not contain any implementations. The Derived classes must overwrite them.

In addition, an abstract class can be derived from an abstract class. It can overwrite the abstract methods of the base class or overwrite them. If not, its derived class must overwrite them.

Ii. interface:
The interface is of reference type, similar to a class, and similar to an abstract class, so many people have vague differences between abstract classes and interfaces. There are three similarities with abstract classes:
1. It cannot be instantiated;
2. contains an unimplemented method statement;
3. The derived class must implement unimplemented methods. The abstract class is an abstract method, and the interface is all members (not only the methods include other members). In addition, the interface has the following features:
In addition to methods, interfaces can also contain attributes, indexers, and events, and these members are defined as common. It cannot contain any other Members, such as constants, fields, constructors, destructor, and static members.
A class can directly inherit multiple interfaces, but can only inherit one class (including abstract classes ).

Interface instance
Public delegate void Del ();
Public interface ITest
{
// Int? A = null;

Int
{
Get;
}

Void Test ();
Event Del OnDel;
Int this [int index]
{
Get;
Set;
}
}
Note! Another type cannot be instantiated:
All constructors are marked as private and cannot be instantiated. Strictly speaking, they cannot be instantiated outside the class, it can be instantiated inside this class (this method can be used to implement the single-piece design pattern ). Note that such classes cannot be inherited as base classes.

Iii. Use of abstract classes and interfaces:
Abstract classes are used to partially implement a class, and users can extend and improve it according to their needs. interfaces only define a behavior specification or provision.
Abstract classes provide common implemented functions across all implementations of components. interfaces are used to create a wide range of different objects.
Abstract classes are mainly used for closely related objects. interfaces are suitable for providing general functions for unrelated classes.
Abstract classes are mainly used to design large functional units, while interfaces are used to design small and concise functional blocks.

For example:
Window forms can be designed with abstract classes. Public operations and attributes can be put into an abstract class, so that the forms and dialogs can inherit from this abstract class and then be expanded and improved according to their own needs.
The print operation can be provided as an interface to each form that requires this function. because the content of the form is different, you must implement your own printing function according to their own requirements. You can call this function only through the interface, instead of printing the form.

-----------------------

II.

What is the difference between a class and an interface?
I picked it from the book, but it is easy to understand:
1. abstract classes are incomplete classes and need to be further specialized. interfaces are just a specification or provision of behavior;
2. The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called;
3. A class can implement several interfaces at a time, but only one parent class can be extended.
4. interfaces can be used to support callback, but inheritance does not.
Some are the differences between interfaces and classes. Fourth, I don't quite understand ......
To implement an interface, a class must be declared as an abstract class unless it implements all the methods promised in the interface (that is, it is not implemented or only implements some methods. the subclass of this class also inherits this feature.

 

 

1. abstract classes are incomplete classes and need to be further specialized. interfaces are just a specification or provision of behavior;
2. The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called;
3. A class can implement several interfaces at a time, but only one parent class can be extended.
4. interfaces can be used to support callback, but inheritance does not.


If you want to create multiple versions of a component, create an abstract class. Abstract classes provide a simple way to control component versions. By updating the base class, all inheritance classes are automatically updated with the change. On the other hand, the interface cannot be changed once it is created. If you need a new version of the interface, you must create a new interface.
If the created function is used across a wide range of different objects, the interface is used.
Abstract classes are mainly used for closely related objects, and interfaces are most suitable for providing general functions for irrelevant classes.
If you want to design small and concise functional blocks, use interfaces.
If you want to design a large functional unit, use an abstract class.
If you want to provide common implemented functions among all the implementations of a component, use an abstract class. Abstract classes allow partial implementation classes, while interfaces do not include any member implementations.

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.