The difference between an abstract class and an interface

Source: Internet
Author: User

1, the class is the abstraction of the object, and the abstract class is the abstraction of the class. The interface is just a specification or a rule of behavior.

2. A class can implement several interfaces at a time, but only one parent class (inheriting a parent class).

3. If an abstract class implements an interface, the method in the interface can be mapped into an abstract class as an abstract method without having to implement the interface method in the subclass of the abstract class.

4, interfaces and abstract classes can not be instantiated, both contain non-implemented method declarations, derived classes must implement a method that is not implemented, abstract classes are abstract methods, and interfaces are all members (methods, other members).

5, abstract class declaration, the interface with the interface declaration, the interface name to add ' I ', the interface in front of the method or property can not have modifiers, methods no method body.

    1. Abstract classes can have non-abstract methods, but the interface can only have abstract methods to declare the existence of the method and not to implement its class is called the extraction class (abstract class), it is used to create a class that embodies some basic behavior, and for the class declaration method, but not in the class implementation of the class. An instance of the abstract class cannot be created. However, you can create a variable whose type is a drawing class and point it to an instance of a specific subclass. You cannot have a smoke-like constructor or a static method of pumping. The subclasses of the Abstract class provide implementations for all the extraction methods in their parent class, otherwise they are also smoke-like classes. Instead, implement the method in the subclass. Other classes that know their behavior can implement these methods in the class.

    2. Interface (interface) is a variant of the extraction class. In an interface, all methods are drawn. Multiple inheritance can be obtained by implementing such an interface. All the methods in the interface are drawn, none of them have a program body. An interface can only define static final member variables. The implementation of an interface is similar to a subclass, except that the implementation class cannot inherit the behavior from the interface definition. When a class implements a special interface, it defines the method (which is given by the program body) to all such interfaces. It can then invoke the interface on any image of the class that implements the interface. Because of the extraction class, it allows you to use the interface name as the type of the reference variable. The usual dynamic binder will take effect. A reference can be converted to an interface type or converted from an interface type, and the instanceof operator can be used to determine whether an object's class implements an interface

The difference between an abstract class and an interface

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.