Abstract classes, interface differences, and abstract classes can not implement all methods of interfaces

Source: Internet
Author: User

Differences between abstract classes and interfaces

Abstract classes can contain non-abstract methods, and interfaces can only contain abstract methods.

Abstract classes cannot contain braces when declaring abstract methods, but all methods in the interface do not.
 
Abstract class ):

A. abstract classes are only for derivation. An abstract class cannot be instantiated. Only classes derived from it can be instantiated.

B. The main feature of an abstract class is that it contains abstract members. Abstract members are non-implemented methods or attributes, and their role is to force all derived classes to provide implementations.

C. Since abstract members should be overwritten, such members will automatically become virtual members and cannot be declared in this way.

D. Abstract members cannot be private; otherwise, they cannot be seen in the derived class.

 
Interface ):

A. The interface is a variant of the abstract class.

B. A key feature of an interface is that it does not contain implementation or data.

C. Fields (that is, data) cannot appear in an interface. If an interface requires a derived class to contain specific data, it uses attributes instead of fields. Because the attribute does not contain any implementation as part of the interface declaration, it does not reference a supported field.

D. The purpose of an interface is to define a contract to be followed by multiple classes. Therefore, all the members of the interface must be of the Public type.

 
Abstract classes do not need to implement all methods of interfaces.

Sometimes the interface and abstract class need to be used together, which can provide developers with considerable convenience and developers can choose which one is convenient. Such abstract classes are called convenience classes. In this case, the convenience class does not need to implement all methods of the interface, but can be left to the subclass that inherits it to implement them.

This is not meaningless. When you write a class that wants to use some methods in the interface (note that not all methods ), then you can use an abstract class to implement this interface first (the method body is empty), and then use your class to inherit this abstract class, so that you can achieve your goal, if you directly use classes to implement interfaces, all methods must be implemented.

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.