Java Learning (13): The difference between abstract classes and interfaces, their pros and cons

Source: Internet
Author: User

The difference point between Java abstract classes and interfaces

1, the interface can only contain abstract methods, the abstract class can contain partial implementation methods.

2, the interface can only define static constants, the default is public static final, and need to display initialization. You can define normal member variables in an abstract class.

3, the interface can not contain the construction method, the abstract class may have its own construction method (inherited use).

4, interfaces and abstract classes cannot be instantiated.

5, a class can implement multiple interfaces, can only inherit an abstract class (can inherit only one class, single inheritance)

6, a class implementation of the interface needs to implement all methods. Inheriting an abstract class requires implementing all of the abstract methods, otherwise it needs to be declared as an abstract class. (If a class contains abstract methods, the class must be declared as abstract class)

7. If a new concrete method is added to an abstract class, all its subclasses are automatically given this new method. If you add a new method to an interface, all classes that implement the interface must be modified synchronously to implement this method, otherwise they cannot be compiled.

8, the implementation of the abstract class can only be given by its subclasses, and Java is a single inheritance, so the abstract class as a type definition tool performance is greatly compromised. Any class that implements an interface can have a type of this interface, and a class can implement multiple interfaces at the same time, so that the class has multiple types.

Java Learning (13): The difference between abstract classes and interfaces, their pros and cons

Related Article

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.