Crazy Java Handouts (14)----abstract classes and interfaces

Source: Internet
Author: User
Tags instance method modifiers

    • Abstract class

Abstract methods and abstract classes must be defined using the abstract modifier, and classes with abstract methods can only be defined as abstract classes, and abstract classes may not have abstract methods. Abstract classes cannot be instantiated.

    • Interface

An interface is just a specification that defines the specifications that a batch of classes need to follow, the interface does not care about the internal state data of these classes, and does not care about the implementation details in these classes, it only stipulates that these classes must provide certain methods. The interface embodies the design philosophy of specification and realization separation.

Interface Syntax:

    1. Modifiers can be public or omitted, and if the modifier is omitted, the package permission access control is used by default. So it's generally public. Because an interface is a common behavior specification for multiple classes, all members of the interface are public access rights.
    2. An interface can have multiple direct parent interfaces, but interfaces only inherit interfaces and cannot inherit classes.
    3. The interface does not contain constructors and initialization block definitions, and can contain a constant attribute/abstract instance method/inner class (internal interface/enumeration) definition.
    4. The constants in the interface are related to the interface, so the system automatically adds static and final modifiers to those properties. The method in the interface automatically adds an abstract adornment. And the modifier of the method can only be public and abstract
    5. The methods in the interface are all abstract methods, so it is not allowed to define static methods, that is, you cannot use static to modify the methods in the interface.

After a class implements one or more interfaces, the class must fully implement all of the abstract methods defined in those interfaces, otherwise the class will retain the abstract method inherited from the parent interface, which must also be defined as an abstract class.

Crazy Java Handouts (14)----abstract classes and interfaces

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.