Deep understanding of Java interfaces and abstract classes

Source: Internet
Author: User

Reprint please indicate the original link:http://www.cnblogs.com/dolphin0520/p/3811437.html  for object-oriented programming, abstraction is one of its major features. 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 of this article: I. Abstract class two. Interface three. The difference between abstract classes and interfaces   before learning about abstract classes, let's 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: The abstract void Fun () Abstraction method 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. [public] abstract class ClassName {abstract void 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 differences between abstract and ordinary classes: 1) The abstract method must be public or protected (because if it is private, it cannot be inherited by the subclasses, 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 subclass must implement the abstract side of the parent classMethod. 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. 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: [public] Interface the InterfaceName {} 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 allow a class to follow a specific set of interfaces requires the use of the Implements keyword, in the following format: Class ClassName implements interface1,interface2,[....] {} You can see that 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. 1. Grammatical differences 1) abstract classes can provide implementation details for member methods, while interfaces can only have public abstract methods, and 2) member variables in an abstract class can be of various types, while 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. Design-level differences 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. In the design, the aircraft can be designed as a class airplane, the bird is designed as aClass Bird, but it is not possible to design the flight feature as a class, so it is just a behavioral trait and not an abstract description of a class 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) to put the three functions in the interface, the need to use the alarm function of the class need to implement this interfaceOpen () and close (), perhaps this class simply does not have the two functions of open () and close (), such as fire alarms. 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 implemen TS Alarm {void oepn () {//...} void Close () {//...} void Alarm () {//...}} Reference: http://blog.csdn.net/chenssy/article/details/12858267http://dev.yesky.com/436/7581936.shtmlhttp:// blog.csdn.net/xw13106209/article/details/6923556http://android.blog.51cto.com/268543/385282/http:// peiquan.blog.51cto.com/7518552/1271610

In-depth understanding of Java interfaces and abstract classes

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.