Deep understanding of Java interfaces and abstract classes

Source: Internet
Author: User

Deep 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

If there are any shortcomings, please understand and welcome the criticism, not very grateful.

Please respect the author's labor results, reproduced please indicate the original link:

Http://www.cnblogs.com/dolphin0520/p/3811437.html

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:

1 abstractvoidfun();

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 abstract classes contain methods that are not specifically implemented, you cannot create objects with abstract classes.

Here's a question: In the Java 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.

123 [publicabstractclass ClassName {    abstract voidfun();}

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:

123 [publicinterfaceInterfaceName {}

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:

12 classClassName implementsInterface1,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.

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:

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() {      //....    }}

Resources:

http://blog.csdn.net/chenssy/article/details/12858267

Http://dev.yesky.com/436/7581936.shtml

http://blog.csdn.net/xw13106209/article/details/6923556

http://android.blog.51cto.com/268543/385282/

http://peiquan.blog.51cto.com/7518552/1271610

Deep understanding of Java interfaces and abstract classes

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.