Differences between abstract classes and interfaces ...... Differences between static methods and instance methods

Source: Internet
Author: User
Class can execute many interfaces, but there is only one parent class. Some people say that using interfaces can implement multi-inheritance. 1.> What are the advantages of abstract classes and interfaces in design?
The interface cannot execute any methods, but the abstract class can.
Class can execute many interfaces, but there is only one parent class. Some people say that using interfaces can implement multi-inheritance.
The interface is not part of the class hierarchy. Classes without Association can execute the same interface.

For details:
Interfaces are descriptions of object attributes and methods (but not their specific implementations). For example, if the Int32 type implements the IConvertible interface, it indicates that Int32 has the ToDouble () method described by the IConvertible interface. However, IConvertible does not provide the content to implement ToDouble (), which must be implemented by the Int32 type.
The interface is used to implement polymorphism. For example, the Int32, Int64, and Single types all implement the IConvertible excuse, which means they must all have the ToDouble () method. Therefore, define a variable of the IConvertible type:
IConvertible c;
Then, whether it is to assign c any variable that implements the IConvertible type, I can ensure that c has at least one ToDouble () method available, without worrying about the specific type of variable c. For example
Int I = 3; // Int32
Long j = 6L; // Int64
Float k = 4F; // Single

Then
C = I;
C. ToDouble ();

C = j;
C. ToDouble ();

C = k;
C. ToDouble ();
Will not cause errors.

The class that implements the interface can explicitly implement the members of this interface. When a member is explicitly implemented, the member cannot be accessed through a class instance, but can only be accessed through the instance of this interface.
Most of the interface applications are used when designing Pattern.


Abstract class, extracted from multiple objects "commonalities", and its descendants have both commonalities and characteristics. For example, a "graph" is abstract and has no shape. it is composed of vertices. a square or a circle is his-derived and can be an object.


2. differences between static methods and instance methods
Static method is a class method, and instance method is a class object method.

For example:
Class
{
Public static void F1 (){};
Public void F2 (){};
}

You can use F1 A. F1 () in this way, but A. F2 () will fail.
F2 can be used in this way,
A a = new A (); a. F2 (); but a. F1 () also has an error.

(Over)

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

In my opinion, abstract classes and interfaces are essentially the same, that is, defining some common methods. As long as a class inherits from an abstract class or implements an interface, it must fully implement all methods in this abstract class or interface.
Both of them coexist because only a single class inheritance is supported, but multiple interfaces can be implemented to implement functions similar to multiple inheritance.
---------------------------------------------------------------

The basic differences are:

An interface is a protocol and a convention. Once published, it cannot be changed. you can only add functions by defining new interfaces. The members defined in the interface cannot be implemented in the interface. All the members in the interface are "abstract.

Abstract classes can be partially implemented. And add new members at will, which is used in a published
Interface.

Of course, if you want to start the project again, everything can be changed. However, a large number of interfaces in. Net released by Microsoft will never be changed. This is the interface. The new version of. Net can enhance some classes, including abstract classes, without affecting the running of your original code on the new version. The interface changes may cause you to rewrite the code.
---------------------------------------------------------------

Why can't static methods in this class be called ???
---------------------------------------------------------------

1. Abstract classes are more focused on summarizing the common features of sub-classes of the same parent class. if the attributes and methods are used, interfaces are more focused on defining methods for any class with the same semantics.
2. The instance method can be seen as the method of an object, and this object belongs to your application. the static method can be seen as a method of your application, if you pull the lens a little farther and think of your application as an object, the static method is a method of the instance of the application. As to why there are static methods, I think that when a class method does not involve some specific data to be instantiated during execution, it can be classified as a static method.

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.