Java interfaces and abstract class differences

Source: Internet
Author: User

turn from: in-depth understanding of Java interfaces and abstract classes

Abstract is one of the most important features of object-oriented programming. In Java, there are two ways to embody the abstraction of OOP: interfaces and abstract classes. There are too many similarities, and there are too many different places. Many people think they can be used interchangeably when they are beginners, but not in practice. Today we will learn about interfaces and abstract classes in Java. The following is the directory outline for this article:

I. Abstract class

Two. Interface

Three. The difference between abstract classes and interfaces

I. Abstract class

Before you learn about abstract classes, take a look at the abstract methods. An abstract method is a special method: it is only declared, and there is no specific implementation. The declarative format for an abstract method is:

Abstract void fun ();

Abstract methods must be decorated with the abstract keyword. If a class contains abstract methods, it is called an abstract class, and the abstract class must be decorated with the abstract keyword before the class. Because an abstract class contains a method that is not implemented, you cannot create an object with an abstract class.

Here's a question: In theJava programming idea, the abstract class is defined as "the class containing the abstract method", but later it is discovered that if a class does not contain an abstract method, it is also an abstract class if it is simply decorated with an abstraction. That is, abstract classes do not necessarily have to contain abstract methods. Personally think this belongs to the problem of the dead, because if an abstract class does not contain any abstract method, why design as abstract class? So remember this concept for a while, without having to delve into why.

[publicabstractclass  ClassName {    abstractvoid fun () ;}

As can be seen here, abstract classes exist for inheritance, if you define an abstract class, but do not inherit it, it is tantamount to creating this abstract class in vain, because you can not use it to do anything. For a parent class, if one of its methods is implemented in the parent class without any meaning, it must be implemented differently according to the actual needs of the subclass, then the method can be declared as an abstract method, and this class becomes the abstract class.

A class that contains an abstract method is called an abstract class, but it does not mean that there can be only abstract methods in an abstract class, which, like normal classes, can also have member variables and ordinary member methods. Note that 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.

Two. 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. In Java, the form of an interface is as follows:

[publicinterface  InterfaceName {}
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 methods are implicitly specified as public Abstract method and can only be public abstract method (with other keywords, such as private, protected, static, final and so on will be reported compile errors), and the interface of all methods can not have a concrete implementation, that is to say, 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.

To have a class follow a specific set of interfaces requires the use of the Implements keyword, in the following format:

class Implements interface1,interface2,[....] {}

As you can see, a class is allowed to follow multiple specific interfaces. If a non-abstract class follows an interface, you must implement all the methods in that interface. Abstract methods in the interface may not be implemented for abstract classes that follow an interface.

Features of the interface:

1. The variable decoration in the interface can only be modified with public or static or final, the others will error, the default is public.

2. The method decoration in the interface can only be decorated with public or abstract, the others will error, the default is public.

3. The construction method cannot be defined in an interface class, and a code block or a static code block cannot be defined.

4. All methods in an interface class cannot be implemented.

5. An abstract class implementation interface can not implement methods in the interface. Interface can not implement interface

three. The difference between abstract classes and interfaces

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 {    publicabstractvoid  open ();      Public Abstract void close ();}

Or:

Interface Door {    publicabstractvoid  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.

InterfaceAlram {voidalarm ();} Abstract classDoor {voidopen (); voidclose ();} classAlarmdoorextendsDoorImplementsAlarm {voidOEPN () {//....    }    voidClose () {//....    }    voidAlarm () {//....    }}

Java interfaces and abstract class differences

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.