The difference between Java abstract class and interface

Source: Internet
Author: User
Tags abstract definition

The difference between Java abstract class and interface

1. Abstract class and interface are two mechanisms in the Java language to support the definition of abstract classes

2. Take the abstract concept of door as an example, the door usually has open and close action, if only these, by the abstract class or interface to define all can, it seems that no problem.

3. But door should also have some abstract properties, such as state, size, shape, material, etc., which are not appropriate in the interface definition (Note: the member variable of interface must be public static final), should be used abstract class to define, so the abstract definition of the same thing/problem domain should be in abstract class, such as birds, shapes, etc.

4. Let's think about how to do a Alarmdoor class with alarm function, in the abstract class Riga alarm () method is not appropriate, because not all doors have alarm function, and can not define the abstract class alarm, Because Java does not allow multiple inheritance, this should be defined with interface, Alarmdoor implement Ialarm interface. So for a specific class to implement some of the new features, can be standardized by interface. The standardized Ialarm interface can also be used for other classes that require alarm functions, such as Alarmwindow.

The difference between Java abstract class and 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.