What is the difference between a switch interface and an abstract class

Source: Internet
Author: User

What is the difference between an interface and an abstract class

What is the basis of your choice to use interfaces and abstract classes?


The concepts of interfaces and abstract classes are different. An interface is an abstraction of an action, and an abstract class is an abstraction of the root.

The abstract class represents what this object is. The interface represents what this object can do. For example, men, women, these two classes (if it's a class ...) ), their abstract class is human. Description, they are all human.

People can eat, dogs can eat, you can define "eat" as an interface, and then let these classes to implement it.

Therefore, in high-level languages, a class can inherit only one class (abstract class) (just as people cannot be both biological and non-living), but can implement multiple interfaces (eating interfaces, walking interfaces).

1th. An interface is a variant of an abstract class, and all methods in an interface are abstract. Abstract classes are classes that declare the existence of a method without implementing it.
2nd. Interfaces can inherit multiple, abstract class not
3rd. An interface definition method cannot be implemented, whereas an abstract class can implement a partial method.
4th. The basic data type in the interface is static and the suction class is not.

When you focus on the nature of a thing, use an abstract class, and when you focus on an operation, use an interface.

Abstract classes are much more powerful than interfaces, but the cost of defining abstract classes is high. Because of the high-level language (which is actually designed), each class can inherit only one class. In this class, you must inherit or write all of its subclasses.

All commonalities. Although the interface is much weaker in functionality, it is only a description of an action. And you can implement multiple interfaces in one class at the same time. In the design phase will reduce the difficulty.

What is the difference between a switch interface and an abstract class

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.