Differences between image classes and interfaces

Source: Internet
Author: User

One is used on the class and the other is used on the behavior.

 

Interface
1. Multi-Inheritance
2. Only definitions can be defined and cannot be implemented.
Abstract class
1. Do not inherit more
2. It can be defined or implemented.
------------
Conclusion abstract class = (interface + class)-inheritance of interfaces
Note: Multi-inheritance is very important. There are many ready-made classes. If you want to extend them, you can only use interfaces instead of interfaces.
Abstract class or common class, because these classes have other display base classes such as form1: form.
Therefore, interfaces are lightweight, while abstract classes are heavyweight implementations.

 

 

To put it simply,
The interface is public, and there cannot be private methods or variables in it. They are used by others. abstract classes can have private methods or private variables,
In addition, the interface must implement all the methods defined in the interface, and the methods to be used can be selectively rewritten to implement the abstract class. In general applications, the top level is the interface, then the abstract class implements the interface, and the class is implemented.
In addition, an interface can implement multi-inheritance, while a class can only inherit one super class, but it can implement multi-inheritance by inheriting multiple interfaces. The interface also has an identifier (there is no method in it, such as the remote interface) and Data Sharing (the variables inside are constants.
1. abstract class represents an inheritance relationship in Java. A class can only use an inheritance relationship once. However, a class can implement multiple interfaces.
2. abstract class can have its own data members or non-Abstarct member methods. In interface, only static data members that cannot be modified (that is, they must be static final, but data members are not defined in interfaces) are allowed. All member methods are abstract.
3. abstract class and interface have different design concepts. Abstract class represents the "is-a" relation, and interface represents the "like-a" relation.
4. classes that implement abstract classes and interfaces must implement all the methods in them. Abstract classes can have non-abstract methods. There is no implementation method in the interface.
5. the variables defined in the interface are of the public static final type by default and must be given the initial values. Therefore, the Implementation class cannot be redefined or changed.
6. the variables in the abstract class are friendly by default. Their values can be redefined in the subclass or assigned again.
7. The methods in the interface are public and abstract by default.
==========================================
Abstract methods are required. It's like animals need to breathe. But the fish breathe with the Gill, and the pigs breathe with the lung. Animals must have breathing methods. How to breathe is a subclass.
At present, there are many discussions and suggestions that use interfaces instead of abstract classes. In theory, the two can be used in a general mix, but they are still different in practical applications. Abstract classes are generally used as common parent classes to provide the foundation for subclass extension. The extension here includes attributes and behavior. In general, the interface does not consider attributes, but only methods, so that sub-classes can freely fill or extend the methods defined by the interface, just as the event adapter mentioned by the Java Prince is a good application.
Using a simple example, for example, a teacher, we take it as an abstract class and have its own attributes, such as age, education level, and instructor number. Teachers are also divided into many types, we can continue the category class and extend the unique category attributes, while the general attributes have been directly continued.
What about interfaces ~ Taking teachers as an example, there are many behaviors of teachers. Apart from being the same as ordinary people, there are also career-related behaviors, such as modifying exams and giving lectures, we define these actions as a non-body method as a set, which is an interface. In contrast, Zhang sanli and Si have different behavioral characteristics, so they can expand their own behavior bodies. In this sense, interfaces focus on behavior.
In short, in many cases, the interface can indeed replace the abstract class, if you do not need to deliberately express the continuation of the attribute.

 

Nowadays, in the face of a large amount of redundant information, it is impossible to directly filter out simple and useful information to ask such questions. In particular, such a conceptual thing, clever memory is also crucial.
I can't remember this application before, but a piece of information on the network is critical:
Abstract class and interface have different design concepts. Abstract class represents the "is-a" relation, and interface represents the "like-a" relation.
Abstract has an essential meaning of reaction. For example, a cat is an animal and a dog is an animal. Therefore, you can write an abstract class: an animal, and then both the cat and the dog inherit the abstract class of an animal.
An interface has a behavioral meaning and more specific meaning. For example, it defines an interface: behavior, which has many methods: Play, eat, and sleep, and then the cat and the dog implement the behavior interface, they all need to implement specific methods: how to play, how to eat, and how to sleep.
There is a concept in design: In a general application, the top-level interface is the interface, then the abstract class implements the interface, and finally the specific class implementation.
The above example design idea is: first define the behavior interface, then define the abstract class animal to implement the behavior interface, and then to the specific class to implement the animal abstract class.
Each person has a different way of remembering. It is critical to choose a proper method to remember it.

Class is the abstraction of objects.
An abstract class is an abstraction of a class.
An interface is an abstraction of behavior.

Abstract classes and interfaces have many similarities:
An interface represents an agreement between the caller and the designer. When multiple people develop the same project in cooperation, defining interfaces to call each other in advance can greatly improve the development efficiency. Interfaces are implemented using classes. classes that implement interfaces must implement all functions provided by interfaces in strict accordance with the declaration of interfaces. With the interface, you can modify the internal implementation of the interface without affecting the existing interface declaration to minimize compatibility issues.
When other designers call an interface, they cannot modify the interface definition at will. Otherwise, the project developer's prior agreement will become meaningless. However, you can modify the corresponding code in the class to complete the content to be modified.
Abstract classes are incomplete and can only be used as the base classes of other classes. The biggest difference between an abstract class and other non-abstract classes is that the abstract class cannot be instantiated.
1) abstract classes and interfaces cannot be instantiated.
2) an interface is a fully abstract member set. abstract classes can be fully implemented or partially implemented or not implemented at all, so as to encapsulate general functions of the inherited classes.
3) abstract classes can only be used as base classes. interfaces can be inherited from other base interfaces.
4) The default access method for interface members is public, and the interface member definition cannot contain any access modifier. The abstract class is not
5) the interface cannot declare any types of static members. The abstract class can
6) The interface members must be methods, attributes, indexers, and events, and cannot be implemented. abstract classes can also declare fields and implemented methods and attributes.
7) abstract methods can only be declared in abstract classes.
Using interfaces or abstract classes to provide polymorphism for components mainly involves the following aspects:
1. If you want to create multiple versions of a component, create an abstract class. Abstract classes provide a simple way to control component versions. Update the base class so that all inherited classes are automatically updated. On the other hand, to protect existing systems written to use interfaces, the interfaces cannot be changed once they are created. If you need a new version of the interface, you must create a new interface.
2. If the created function will be used between totally different objects in a wide range, the interface is used. Abstract classes are mainly used for closely related objects, and interfaces are most suitable for providing general functions for irrelevant classes.
3. If you want to design small and concise functional blocks, use interfaces. If you want to design large functional units, use abstract classes. Well-designed interfaces are often very small and independent from each other, reducing the possibility of performance problems.
4. If you want to pass common implemented functions across all implementations of a component, use an abstract class. Abstract classes allow partial implementation classes, while interfaces do not include the implementation of any member.

In summary:
Horizontal interface vertical viewing class: interfaces are used for horizontal scaling; abstract base classes are used for Vertical Scaling; horizontal and vertical scaling, and abstract base classes are used to inherit interfaces.
Interfaces are used when different objects are pursuing Functional consistency;
Abstract concatenation classes are used when different functions are pursued in objects with small differences.

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.