Differences between Java interfaces and abstract classes

Source: Internet
Author: User

The concepts of interfaces and abstract classes are different. The interface is the abstraction of actions, and the abstract class is the abstraction of the source. The abstract class indicates what this object is. The interface indicates what the object can do. For example, men and women (if they are classes ......), Their abstract class is human. It means they are all people. People can eat, dogs can also eat, you can define "eat" as an interface, and then let these classes implement it. therefore, in advanced languages, a class can only inherit one class (abstract class) (just as humans cannot be both biological and non-biological ), however, multiple interfaces (meal and walking interfaces) can be implemented ). First, the interface is a variant of the abstract class, and all the methods in the interface are abstract. Abstract classes are classes that declare the existence of methods rather than implementing them. Second, interfaces can be inherited. abstract classes cannot. Third, interfaces can define methods and cannot be implemented. abstract classes can implement some methods. Fourth, the basic data type in the interface is static, but the image is not. When you focus on the essence of a thing, use abstract classes; when you focus on an operation, use interfaces. Interfaces can be implemented or inherited. abstract classes do not support abstract classes, which far exceed interfaces. However, it is costly to define abstract classes. Because in advanced languages (also in actual design), each class can only inherit one class. In this class, you must inherit or compile all the commonalities of all its sub-classes. Although the interface is much weaker in function, it only describes an action. In addition, you can implement multiple interfaces in a class at the same time. This will reduce the difficulty in the design phase.

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.