The difference between an abstract class and an interface

Source: Internet
Author: User

1. Differences in the grammatical level

1) Abstract classes can provide implementation details of member methods, and only public abstract methods exist in interfaces;

2) member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types;

3) The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

4) A class can inherit only one abstract class, while a class may implement multiple interfaces.

2. Differences at the design level

1) Abstract class is an abstraction of a thing, that is, an abstraction of a class, and an interface is an abstraction of a behavior. An abstract class is an abstraction of the whole class as a whole, including properties, behaviors, but an interface that abstracts the local (behavior) of a class. For a simple example, airplanes and birds are different kinds of things, but they all have a common denominator, that is, they can fly. Then in the design, the aircraft can be designed as a class airplane, the bird is designed as a class bird, but not the characteristics of the flight is also designed as a class, so it is only a behavioral characteristics, not a kind of abstract description of things. At this point the flight can be designed as an interface fly, including the method fly (), and then airplane and bird respectively according to their own needs to implement the fly this interface. Then as for the different types of aircraft, such as fighter jets, civil aircraft, such as direct inheritance airplane can, for birds is similar, different species of birds directly inherit the bird class can be. As can be seen from here, inheritance is a "is not" relationship, and the interface implementation is "there is no" relationship. If a class inherits an abstract class, then the subclass must be the kind of abstract class, and the implementation of the interface is there is no, with no relationship, such as whether the bird can fly (or whether it has the characteristics of flight), can fly can realize this interface, not flying will not implement this interface.

2) The design level is different, abstract class as the parent class of many subclasses, it is a kind of template design. And the interface is a kind of behavior specification, it is a kind of radiant design. What is a template design? The simplest example, we have used the template in ppt, if the template a designed ppt B and ppt c,ppt B and ppt C public part is the template A, if their public parts need to change, then only need to change the template A can be, do not need to re-ppt B and ppt C changes. and radiation design, such as an elevator are installed some kind of alarm, once to update the alarm, you must update all. That is, for an abstract class, if you need to add a new method, you can directly add a concrete implementation in the abstract class, the subclass can not be changed, but for the interface is not, if the interface has been changed, all implementations of this interface must be modified by the corresponding class.

Here's an example of the most widely circulated online: doors and Alarms: doors have open () and close () two actions, at which point we can define this abstract concept through abstract classes and interfaces:

Abstract class Door {public    abstract void Open ();    public abstract void Close ();}

Or:

Interface Door {public    abstract void Open ();    public abstract void Close ();}

But now if we need the door to have the function of alarm alarm (), then how to implement? Here are two ideas:

1) These three functions are placed in the abstract class, but so that all the subclass inherited from the abstract class has an alarm function, but some doors do not necessarily have the alarm function;

2) The three functions are placed in the interface, need to use the alarm function of the class need to implement the interface of open () and close (), perhaps this class does not have open () and close () these two functions, such as fire alarm.

As can be seen here, Door's open (), close () and alarm () are essentially two different categories of behavior, and open () and close () are intrinsic behavior characteristics of the gate itself, and alarm () is an extension of the additional behavior. So the best solution is to individually design the alarm as an interface that contains the alarm () behavior, door is designed as a separate abstract class that contains open and close two behaviors. Then design an alarm gate to inherit the door class and implement the alarm interface.

Interface Alram {    void alarm ();} Abstract class Door {    void open ();    void close ();} Class Alarmdoor extends Door implements Alarm {    void Oepn () {      //...    }    void Close () {      //...    }    void Alarm () {      //...    }}

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