Java object-oriented programming--tenth chapter interface

Source: Internet
Author: User
Tags define abstract

1, the interface can be regarded as a special case of abstract class. Abstract methods can be defined in an abstract class, or specific methods can be defined. However, an interface can only define abstract methods. All interfaces can be viewed as abstractions of behavior. Define the interface using the keyword interface, implementing the interface using the keyword implements.

2. If a method uses a parent class variable as an argument, then any subclass object can be passed. If a method uses an interface variable as an argument, then any one of the implementation class objects can be passed. (polymorphic)

3, the interface can only define abstract methods, so the method in the interface whether or not to write abstract, are abstract methods, can only have the definition of methods. In addition, the methods in the interface are public publicly, and if you do not write public, the default is the publicly available method, and you cannot use protected or private to define the interface method.

4, a class can have more than one interface, separated by "," but an abstract class can inherit only one class.

5, the variables defined in the interface are constants of public static final.

6, the interface can inherit the interface, and can inherit multiple interfaces, a class implementation of an interface, not only to implement all the methods defined in the interface, but also the interface of the parent interface definition method is also implemented.

7. Inheritance and Interfaces:

    • Both the inheritance and the interface are abstract results;
    • Inheritance is used to share common code, reuse methods and properties in related class instances;
    • The Java interface is used to share common behaviors and reuse methods in different class instances;

8. The difference between an interface and an abstract class:

    • Abstract classes and interfaces cannot produce objects (instantiation);
    • All the methods in the interface are abstract, and the definition of both methods and methods is implemented.
    • The variables defined in the interface must be public static constants, whereas the variables defined by the abstract class are ordinary properties;
    • A class can only inherit from another class, but it is possible to implement multiple interfaces;
    • Interfaces can inherit multiple interfaces, but abstract classes can inherit only one root;

9, the application of the interface:

Event Development Steps:

    • 1, determine the source of events;
    • 2, determine the Listener (ActionListener interface);
    • 3, registration, the establishment of the event source and the Listener contact (X.addactionlistener (New MyEvent ());)

Java object-oriented programming--tenth chapter interface

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.