2/interface differs from abstract class

Source: Internet
Author: User


A class that declares the existence of a method and does not implement it is called an abstract class, which is used to create a class that embodies some basic behavior, declares a method for that class, but does not implement the class in that class. An instance of the abstract class cannot be created. However, you can create a variable whose type is an abstract class that points to an instance of a specific subclass. Cannot have abstract constructors or abstract static methods. The subclasses of the abstract class provide implementations for all abstract methods in their parent class, otherwise they are also abstract classes. Instead, implement the method in the subclass. Other classes that know their behavior can implement these methods in the class.
An interface (interface) is a variant of an abstract class. In an interface, all methods are abstract. Multiple inheritance can be obtained by implementing such an interface. All the methods in the interface are abstract, without a program body. An interface can only define static final member variables. The implementation of an interface is similar to a subclass, except that the implementation class cannot inherit the behavior from the interface definition. When a class implements a special interface, it defines the method (which is given by the program body) to all such interfaces. It can then invoke the interface's method on any object that implements the interface's class. Because of an abstract class, it allows you to use the interface name as the type of the reference variable. The usual dynamic binder will take effect. A reference can be converted to an interface type or converted from an interface type, and the instanceof operator can be used to determine whether the class of an object implements an interface.


Also, an interface can implement multiple inheritance, and a class can inherit only one superclass, but it is possible to implement multiple inheritance by inheriting multiple interfaces, and the interface has the function of identifying (there are no methods, such as remote interfaces) and data sharing (the variables inside are all constants).


"An interface is a fully abstracted collection of members that cannot be implemented at the interface definition, and we can treat it as defining the contract for the operation, and the implementation of the interface is left entirely to the developer." The difference between them, if careful analysis, still have a lot of: in Java, a class can only inherit from a base class, so if you want to use an abstract class to provide polymorphism for a set of classes, these classes must be inherited from that class, interface is not the same, it can not only use a class or structure to implement multiple interfaces, An interface can also have multiple implementations. ”

An abstract class is a class that cannot be instantiated and must inherit from it. Abstract classes can be fully implemented, but more commonly, partial implementations, or not at all, encapsulate the common functionality of inheriting classes, which can provide implemented members, so you can use abstract classes to ensure a specific number of identical functions, but not with an interface.
In other words, are they different in the question of providing polymorphism? "I seem to have understood something.
"If you expect to create multiple versions of a component, we should create an abstract class. This is because abstract classes provide a simple way to control the component version. By updating the base class, all inherited classes are automatically updated with the changes. This is a good thing, and of course it's a problem. On the other hand, the interface cannot be changed once created. If a new version of the interface is required, a completely new interface must be created. Therefore, if you create a feature that will be used across a wide range of heterogeneous objects, the interface is used. "------:" Can you say that abstract classes are primarily used for closely related objects, and interfaces are best suited to provide common functionality for unrelated classes. ”
I told you this morning that to create a control, the first thing to do is to implement some interfaces to allow the system to be recognized (see the previous "interface"). The linkage between controls is not very relevant. Therefore, they are mostly based on interfaces. However, one thing to note is that when designing a component, you use an abstract class if you want to provide common, implemented functionality across all implementations of a component. This is because of the reason we have just said that abstract classes allow partial implementations of classes, and interfaces do not contain implementations of any members. ”
"Well, see, the difference between them is a little bit clear. "I nodded silently.

"In addition, there is a common design idea, if you want to design a small and concise function block, then use the interface." If you are designing large functional units, use an abstract class. ”
"It seems that the design of the problem is quite large, in general, how to design the interface?" "I went on to ask.
"Why do you see programming books, program routines, very few interface descriptions, and examples of class implementations abound?" This gives us a wake up from one side, and if used properly, the interface can be a useful tool. But if used improperly, they can become very tricky and even hinder effective programming. The design and use of the interface is actually a brilliant art. ”

, "through the interface and implementation, we can apply the same type of program on different objects, and do not have to modify the original class, relative subroutines must be modified by the source code to achieve the purpose of reuse, interface and implementation is not only a great progress, but also a very high level of procedural design art. ”

"But the biggest problem is focusing on the interface design. Once an interface is defined and accepted, it must remain intact to protect applications written for use with that interface. After the interface is published, it cannot be changed. This is an important principle of our component design, called ' Interface invariance '. ”

"I have repeatedly emphasized that creating an interface is a definition that can never be changed after the interface definition is published. Interface invariance is to protect an existing system that is written to use the interface. We should create a new interface when the interface design is in contact with the requirements and the confirmation needs to be drastically changed. The general naming method is that the new interface can be named by appending a number ' 2 ' to the original interface name to show its relationship to the existing interface. The new interface is then created through interface inheritance. ”

"But if the demand analysis is not good, will there be a lot of derived interfaces?" "I'm a little worried.

"This is positive, and generating new interfaces too often makes the components very large because of the unused interface implementations." Experienced designers, after sufficient analysis of the requirements, the design of the interface is often small and independent of each other, reducing the possibility of performance problems. ”

"This ability to decompose is really art!" "I can't help but be amazed by it.

"Of course, in general, we will identify which properties and methods are part of the interface design process called ' interface decomposition '. The basic guideline is that several tightly related functions should be aggregated into a group in one interface. Too many features will make the interface inconvenient to run, and too much subdivision will result in additional overhead and reduced ease of use. The ability to master this degree of decomposition requires constant technical discipline, as well as an in-depth analysis and understanding of each project. ”

What are the benefits of interfaces compared to class implementation inheritance? "I tried to say it, in general, the interface is a very efficient programming method that separates object definitions from implementations so that objects can be perfected and evolved without destroying existing applications." Interfaces eliminate a big problem with implementing inheritance, which is likely to corrupt your code when you make changes to it after the design is implemented. Even if implementing inheritance allows classes to inherit implementations from the base class, the first time the class is released will still make us have to make a lot of choices for the design. If the original assumptions are incorrect, it is not always possible to make safe changes to the code in a later release. For example, we define a method for a base class that requires an Integer parameter, and later determines that the parameter should be a Long data type. We cannot safely change the original class because an application designed for a class that derives from the original class may not compile correctly. This problem can be magnified because a single base class affects hundreds of subclasses. ”

"Is it possible to solve this problem by overloading the original class with a long type of argument?" Big Li raised a question.

"This one?" "I thought about it," however, this does not necessarily achieve satisfactory results, because a derived class might need to override a method that takes an integer, which might not work correctly if a method that takes a Long data type cannot be overridden. ”

"What do you do with the interface?" "Big Li does not want to continue to ask without scratching."

"The solution is to publish an update interface that accepts new data types. "I answered it in a sudden.

"It seems you have mastered the basic aspects of the interface operation." "Big Li's comments really make me happy." "I'll summarize for you that the main reasons for using interface inheritance instead of class inheritance are: The applicability of the interface is stronger when the application requires many potentially unrelated object types to provide some functionality, and the interface is more flexible than the base class because a single implementation can be defined to implement multiple interfaces The interface is better without inheriting the implementation from the base class, which is useful in situations where class inheritance cannot be used. For example, structs cannot inherit from classes, but they can implement interfaces. ”

2/interface differs from abstract class

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.