Java thousands ask _05 object-oriented (005) _ Interface and abstract class what is the difference

Source: Internet
Author: User
Tags abstract definition what interface

Click to enter _ many other _java thousand ask

1. What is the difference between an interface and an abstract class?

In the Java language. Abstract class and interface interface are two mechanisms of abstract definition.

It is the existence of these two mechanisms that gives Java a powerful object-oriented capability. Abstract class and interface interface are very similar in terms of abstraction definitions. Can even replace each other. So a lot of developers are more selective when it comes to abstract definitions. In fact, there is a very big difference between the two. Their choice can reflect the understanding of the nature of the problem and the understanding of the design intent.

Learn about Abstract classes here: How abstract concepts in Java are embodied
Understanding the interface look here: What exactly is the interface in Java
Learn a lot about other inheritance see here: what is the meaning of Java class inheritance?

Details such as the following:

Learn about the is-a,has-a,like-a relationship look here: what is is-a,has-a,like-a

2, interface application in what occasions
    1. Classes and classes require a specific interface to reconcile, regardless of how they are implemented.
    2. As an identity that is capable of implementing a specific function, it can also be a purely identifier for what interface methods do not exist. such as serialized interface: Serializable
    3. A set of classes needs to be treated as a single class, and the caller is only connected to this set of classes through an interface.
    4. There is a need to implement a specific number of functions that may not have any connection whatsoever.

3. What is the application of abstract class?
    1. Defines a set of interfaces, but does not want to force every implementation class to implement all interfaces.

      The ability to define a set of method bodies, even an empty method body, with the abstract class, and then select the methods that are of interest to the subclasses to overwrite them.

    2. Some occasions. A purely interface does not satisfy the coordination between classes and classes, and the attributes that represent states in a class are required to discriminate between different relationships.

    3. Standardize a set of mutually coordinated methods. Some of these methods are common. State-Independent, sharable, without subclasses, and there are methods that require individual subclasses to implement specific functions based on their own specific state.

Java thousands ask _05 object-oriented (005) _ Interface and abstract class what is the difference

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.