C # interview: Abstract classes and interfaces

Source: Internet
Author: User

I recently encountered such problems in the interview. And then another tutorial for a bit. We hope to help our peers.


First, abstract class:
Abstract classes are special classes that simply cannot be instantiated; Having other characteristics of the class, it is important that abstract classes can include abstract methods, which are not available to ordinary classes. Abstract methods can only be declared in an abstract class, and do not include any implementation whatsoever. Derived classes must overwrite them.

In addition, abstract classes can derive from an abstract class, which can override the base class's abstract method and not overwrite it, assuming it is not overwritten. Then their derived classes must overwrite them.

Second, the interface:
Interfaces are reference types, similar to classes, and are similar to abstract classes with three points:
1, can not be instantiated;
2, including the non-implementation of the method statement;
3. Derived classes must implement methods that are not implemented, abstract classes are abstract methods, and interfaces are all members (not just methods containing other members); The interface has the following characteristics, for example:
In addition to being able to include methods, interfaces can include properties, indexers, events, and those members are defined as public.

In addition, no other members may be included. For example: constants, fields, constructors, destructors, static members. A class can inherit directly from multiple interfaces, but only directly inherit a class (including abstract classes).

Iii. the differences between abstract classes and interfaces:
1. A class is an abstraction of an object that can be interpreted as a class as an object, an abstract class called an abstraction. Whereas an interface is merely a specification or a rule of behavior, Microsoft's own definition of the interface is always followed by the able field, proving that it is a class "I can do."

.. ". Abstract classes many of the others are defined in a series of tightly related classes, while interfaces are mostly in classes where relationships are loose but the functionality is implemented.
      2. The interface basically does not have the inheritance regardless of any detailed characteristic, it only promises the method which can call;     
       3. A class can implement several interfaces at a time, but can only extend a parent class     
      4. Interfaces can be used to support callbacks, and inheritance does not have this feature.     
      5. Abstract classes cannot be sealed.


6. The detailed method of the implementation of the abstract class is implicit, but the interface method in the class that implements the interface is implicitly non-virtual, and of course you can declare it as virtual.
7. (interfaces) are similar to non-abstract classes. An abstract class must also provide its own implementation for all members of the interface listed in the base class list of the class. But. Agree that an abstract class maps an interface method to an abstract method.
8. Abstract classes implement a principle in oop, separating mutable from immutable. Abstract classes and interfaces are defined as immutable. And the variable seat sub-class to achieve.
9. A good interface definition should be functional. Instead of multifunction, it causes the interface to pollute. Suppose a class simply implements one of the functions of the interface, and has to implement the other methods in the interface. It's called interface pollution.
10. Try to avoid using inheritance to implement the build function, but instead use black-box multiplexing. That is, the object combination. As the level of inheritance increases, the most immediate consequence is that when you call one of these classes, you must load all of them into the stack! The consequences are conceivable. (in conjunction with the stack principle). At the same time. A friend with a heart can notice that when Microsoft constructs a class, it uses a method of object composition very often.

For example, in ASP. NET, the page class has properties such as server request. But in fact they are all objects of a class. Using this object of the page class to invoke the methods and properties of another class is a very important design principle.


11. Assume that an abstract class implements an interface. It is possible to map a method in an interface to an abstract class as an abstract method without having to implement an interface method in a subclass of an abstract class.

Iv. use of abstract classes and interfaces:
1. Suppose you are estimating that you want to create multiple version numbers of a component, create an abstract class. Abstract classes provide an easy way to control the component version number.


2. Assume that the functionality created will be used across a wide range of heterogeneous objects. The interface is used. Suppose you want to design a small and concise block of functionality, use an interface.


3. Suppose you want to design a large functional unit. The abstract class is used. If you want to provide common, implemented functionality across all implementations of a component, use an abstract class.
4. Abstract classes are primarily used for closely related objects, while interfaces are suitable for providing common functionality for unrelated classes.

Here are some of the images I saw on the internet, really very good, hehe:

1> plane can fly. Birds can fly, they all inherit the same interface "Fly". But F22 belongs to the airplane abstract class, the pigeon belongs to the bird abstract class.
2> is like the door of Iron Gate (abstract class). You want a door I can't give (can't instantiate), but I can give you a detailed iron gate or wooden door (polymorphic), and only can be the door, you can not say it is a window (single inheritance), a door can have a lock (interface) can also have a doorbell (multi-implementation). The door (abstract class) defines what you are. The interface (lock) specifies what you can do (an interface is best to do only one thing.) You can't ask for a lock to make a sound (interface pollution).

What is the difference between an abstract class and an interface? In simple terms,
The interface is public. There can be no private method or variable, it is for others to use, and abstract class can have private method or private variable. In addition. To implement an interface, it is necessary to implement all the methods defined in the interface, and the implementation of abstract classes can selectively rewrite the required methods. In general applications, the top level is the interface, and then the abstract class implements the interface. Finally to the detailed class implementation. Also, interfaces can implement multiple inheritance. A class can inherit only one superclass, but it can implement multiple inheritance by inheriting multiple interfaces. The interface also has the function of identifying (there are no methods, such as remote interfaces) and data sharing (the variables inside are all constants). The difference between the interface and the abstract class, I think, is mainly two points, one is the abstract class can also have a non-abstract method, although the abstract class still cannot be instantiated. But its subclasses can. This means that these non-abstract functions can still be called by polymorphic states. This is why abstract classes are not completely replaced by interfaces. The interface is the opposite. Just the interface also has an advantage that the abstract class cannot match. Is that a class can inherit multiple interfaces. Abstract classes, like ordinary classes, can inherit only one. So the interface can solve multiple inheritance problems. An abstract class can implement only a subset of the methods, and the interface must implement all of its methods. The methods of abstract classes can have abstract methods and can have common methods. The method in the interface must be an abstract method. The access permission of the method of the abstract class can be many, the access permission of the method of the interface can only be public; the subclass of the abstract class can only inherit an abstract class, and the class implementing the interface can implement multiple interfaces at the same time.
The general point says:
interface = Defines all the things to do. But I'm not doing anything.
Abstract class = Do a part of the co-do, the rest of his quit, and so Yimeimei continue to complete this is the difference!


Members of an abstract class can have an access level, whereas members of an interface have all public levels
Abstract classes can include fields, and interfaces are not able to
An abstract class can inherit an interface, and an interface cannot inherit an abstract class
Members of an abstract class can have a detailed implementation, and interfaces do not
Abstract subclasses can selectively implement abstract methods of their base classes, and the subclasses of an interface must all implement

Transferred from: http://www.360doc.com/content/10/0525/16/1039473_29458325.shtml


C # interview: Abstract classes and interfaces

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.