Java interface and abstract class usage Summary

Source: Internet
Author: User

Interface

1. Because Java does not support multiple inheritance, an interface can inherit only one parent class, but multiple interfaces can be implemented. The interface itself can inherit multiple interfaces.

2. The member variables in the interface are of the public static final type by default. Initialization that must be displayed.

3. All methods in the interface are public abstract by default. Implicit declaration.

4. The interface cannot be instantiated because there is no constructor.

5. The interface cannot implement another interface, but it can inherit multiple interfaces.

If Class 6 implements an interface, all abstract methods in the interface must be implemented; otherwise, the class must be defined as an abstract class.


Abstract class

1If a class is declared as abstract, this class cannot generate objects and can only be inherited and used.

2. Abstract METHODS must exist in abstract classes.

3. the abstract class can contain general variables and general methods.

4. The subclass inherits the abstract class, except that the subclass is an abstract class.
Private void print () {}; this statement indicates the empty Implementation of the method.
Abstract void print (); this statement indicates the abstraction of the method without implementation.


Differences between interfaces and abstract classes

1. An interface can only contain abstract methods. An abstract class can contain common methods.
2. The interface can only define static constant attributes. abstract classes can either define common attributes or static constant attributes.
3. The interface does not contain constructor methods. abstract classes can contain constructor methods.

An abstract class cannot be instantiated, but it does not mean that it cannot have constructor, an abstract class can have constructor, and a backup inheritance class can be expanded.

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.