Abstract classes and Interfaces

Source: Internet
Author: User

Abstract classes and interfaces are two mechanisms for defining abstract concepts in the Java language.

Abstract classes and interfaces used to characterize abstract concepts are not instantiated in the object-oriented domain because abstract concepts have no corresponding specific concepts in the problem domain.

First, abstract class

Abstract class embodies the idea of data abstraction and is a mechanism to realize polymorphism. It defines a set of abstract methods that are implemented by derived classes as a concrete representation of this set of abstract methods. At the same time, abstract classes provide the concept of inheritance, the starting point is to inherit, otherwise it does not exist any meaning. Therefore, the definition of the abstract class must be used to inherit.

As long as an abstract class that contains an abstract method must be defined as an abstract class

Abstract classes can contain specific methods, and of course, they may not contain abstract methods.

The abstract method in the subclass cannot have the same name as the abstract method of the parent class.

Abstract cannot decorate the same class with final. The--final class cannot be inherited, and the abstract class is meant to inherit from a subclass.

Abstract the same method cannot be decorated with private, static, final, or native.

It is useful to create abstract classes and abstract methods, because they can clarify the abstraction of a class and tell the user and the compiler how they intend to use them. Abstract classes are also useful refactorings because they enable us to easily move public methods up and down the inheritance hierarchy.

Second, the interface

Interface is an extension of the abstract class, Java in order to ensure that data security is not multiple inheritance, that is, inheritance can only exist a parent class, but the interface is different, a class can implement multiple interfaces at the same time, regardless of whether there is any relationship between these interfaces, so the interface to compensate for the abstract class can not be multiple inheritance defects , However, it is recommended that inheritance and interfaces be used together, as this ensures both data security and multiple inheritance.

An interface is used to establish a protocol between classes, which provides only a form, without a specific implementation. The implementation class that implements the interface must implement all of the methods of the interface by using the Implements keyword, which indicates that the class is following a particular interface or group of interfaces

All methods of a interface are public abstract by default. The declaration is protected and private will compile an error.

The member variables in interface are public static final by default.

You can use instanceof to check whether an object implements a particular interface.

Be sure to avoid duplicate method names when implementing multiple interfaces.

Three, the difference 3.1 grammatical level

The abstract class "declaration and definition" is similar to a normal class, with an abstract adornment when defining a class. The interface is not a class concept at all, and the interface keyword is used when defining it.

The permission modifier abstract class allows you to use the visible permission modifier (except the private permission modifier). The interface can only be accessed with public or package access, and the default is abstract.

The "Inheritance and implementation" abstract class is used to inherit (extends), the subclass inherits the abstract class can implement some or all of the abstract method, as long as there is an abstract method, the class is still an abstract class, cannot be instantiated. The interface is implemented (implements), and the implementation class must implement all the methods within the interface. Interfaces can only be inherited by an interface, and classes cannot inherit interfaces.

The "About method" abstract method only needs to be declared, not implemented. Abstract classes can have abstract methods and can have common methods. All methods in an interface are abstract by default. Abstract methods in abstract classes, permission adornments cannot use private, others can. The method permission decoration in an interface is allowed only for public or package access permissions.

The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

In the "About member variables" abstract class, you can define a variety of member variables, just like normal classes. However, the member variables defined in the interface are public static final by default, and generally do not define variables in the interface.

3.2 Design Levels

Abstract hierarchy the author thinks that interfaces are more abstract than abstract classes. Abstract classes can be thought of as an abstract form between ordinary classes and interfaces, with less restrictive constraints.

An abstract class is an abstraction of a class, including properties, behaviors. And an interface is an abstraction of behavior.

"Cross-domain" because of the different levels of abstraction, the cross-domain of an interface is much broader than the abstract class. Abstract classes are cross-domain in a class that is specific to similar characteristics, whereas interfaces can span different classes of domains. Because the interface is abstract behavior, and there is no relationship between its implementation class, as long as the implementation of the class requires such a contract, you can implement the interface. Abstract classes are in the inheritance system, which implies the convention of the inheritance relationship.

The "Design level" abstract class is a link in the top-down system, which must have a set of conventions on the inheritance system, such as describing what kind of subclass, what is common. The interface is just a rule, there is no need to know the existence of subclasses, what is the subclass, how to implement it does not need to tube.

Abstract classes are abstracted from the bottom up, and the interfaces are designed from the top down.

Abstract classes are the parent class of many subclasses, and it is a template-like design. And the interface is a kind of behavior specification, it is a kind of radiant design.

For abstract classes, if you need to add a new method, you can directly add a specific implementation in the abstract class, the subclass can not be changed, but for the interface is not, if the interface is changed, all implementations of this interface must be modified.

Iv. ISP (Interface segregation Principle) interface separation principle

If the interface definition of the class exposes too much behavior, the interface definition for this class is not cohesive enough. In other words, the interface of a class can be decomposed into a combination of multiple functional functions, each of which serves different customer classes, and different customer classes can choose to use different functional groupings.

"Interface Pollution problem" if: whenever a derived class needs to use a new interface, it adds the interface to its base class, which can cause interface pollution. Persistent pollution will make the base class interface become more and more bloated.

Because a new interface method is added to the base class definition, all other derived classes of the base class add the method accordingly. In fact, a subclass that does not need the method may not provide the implementation, but instead directly uses the empty implementation in the base class. This design violates the Richter scale substitution principle, reducing the maintainability and reusability of the code.

When customer classes are forced to rely on interfaces that they do not need, these client classes have to be constrained by these interfaces. This inadvertently results in a coupling between all customer classes. In other words, if a customer class relies on a class that contains interfaces that the customer class does not need, but these interfaces are required by other client classes, this modification will also affect the client class when other client classes require that the class be modified. Usually we avoid this coupling as much as possible, so we need to do our best to separate these interfaces.

[Richter Replacement principle] Lsp:the Liskov Substitution Principle

Using a base class object pointer or a referenced function must be able to use the derived class's object without understanding the derived class's condition.

The top-level interface is not dependent on the underlying implementation.

The "Interface separation principle" "customer class should not be forced to rely on interfaces that they do not need."

By applying the Adapter design pattern, using the delegate (delegation) or multi-inheritance approach, the fat interface can be separated into multiple abstract base class interfaces, thus breaking the unnecessary coupling between the customer classes. See http://www.cnblogs.com/gaochundong/p/interface_segregation_principle.html

Five, interface-oriented programming

The main frame uses the interface, the interface forms the skeleton of the system, so it can replace the realization of the system by replacing the implementation class of the interface.

Pending additions

Resources

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

https://yq.aliyun.com/articles/32108

Http://www.cnblogs.com/gaochundong/p/interface_segregation_principle.html

Http://www.cnblogs.com/gaochundong/p/liskov_substitution_principle.html

Abstract classes and Interfaces

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.