The difference between abstract class and interface of Java Foundation

Source: Internet
Author: User

In the process of learning Java, or in the search for a job written or interview, if you are a Java post, then the difference between abstract class and interface is undoubtedly a big hot spot, is the major companies want to test a small knowledge point, the following is my summary of the autumn in September, if there is wrong place, welcome to correct.

Here, draw on the http://www.cnblogs.com/dolphin0520/p/3811437.html, write very good.

First, abstract class

Classes that are decorated with the abstract keyword are called abstract classes. An abstract class cannot be instantiated, that is, an object (instance) of an abstract class cannot be new.

Abstract Method: The method that is decorated with the abstract keyword is called an abstract method. abstract methods need to be defined in an abstract class . In contrast to abstract methods, the methods we define at ordinary times are called concrete methods (declarations, implementations).

If a class contains an abstract method, then the class must be abstract class;

If a class is an abstract class, the class can contain concrete methods (declarations, implementations).

In either case, this class cannot be instantiated as long as the class is an abstract class.

In addition, in the case where the subclass inherits the parent class (the parent class is an abstract class), the subclass must implement all the abstract methods defined in the parent class, otherwise the subclass needs to declare an abstract class.

  

There are three main differences between abstract and ordinary classes:

1) The abstract method must be public or protected (because if you are private, you cannot inherit from the quilt class, the subclass cannot implement the method), and by default it is public.

2) Abstract classes cannot be used to create objects;

3) If a class inherits from an abstract class, the child class must implement the abstract method of the parent class. If the subclass does not implement an abstract method of the parent class, the subclass must also be defined as an abstract class.

In other respects, there is no difference between an abstract class and an ordinary class.

Second, the interface

Interface, English is called interface, in software engineering, the interface refers to the method or function for others to call. From here, we can realize the original intention of the Java language Designer, which is the abstraction of the behavior.

The interface can contain variables and methods. Note, however, that the variables in the interface are implicitly specified as public static final variables (and can only be public static final variables, which are reported as compilation errors with private adornments), and method is implicitly specified as the public abstract method and can only be the public abstract method (with other keywords, such as private, protected, static, final, and so on, the compilation error is reported), And all the methods in the interface cannot have a concrete implementation, that is, the methods in the interface must all be abstract methods. From here we can see the difference between interface and abstract class, interface is an extremely abstract type, it is more "abstract" than abstract class, and generally does not define variables in the interface.

The interface is equivalent to class, and all methods in the interface are abstract methods . When you declare a method in an interface, you can use the abstract keyword or you can not use it. Typically, the abstract keyword is omitted.

An interface can be considered a special abstract class (an interface can only be abstracted by an abstract method, not a specific method).

When a class implements an interface, the class must implement all the methods declared in the interface. If the class is an abstract class, then you do not need to implement the methods in the interface.

Three, the difference between abstract class and interface

1. Differences in the grammatical level

1) Abstract classes have abstract methods can also have specific methods, and the interface can only exist public abstract method, that is, abstract method;

2) member variables in abstract classes can be of various types, whereas member variables in an interface can only be public static final type, that is, constant type;

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. Or it's called single inheritance, multiple implementations .

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:

1234 abstractclass Door {    public abstract void open();    public abstract voidclose();}

Or

1234 interfaceDoor {    public abstract void open();    public abstract voidclose();}

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.

1234567891011121314151617181920 interfaceAlram {    voidalarm();}abstractclassDoor {    voidopen();    voidclose();}classAlarmDoor extendsDoor implementsAlarm {    void oepn() {      //....    }    voidclose() {      //....    }    voidalarm() {      //....    }}

The difference between abstract class and interface of Java Foundation

Related Article

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.