Java abstract classes and interfaces

Source: Internet
Author: User

Interface is a higher level of abstraction than an abstract class, and all methods in an interface are abstract methods without any code for implementation.

1. The method of interface can only be public abstract. (public abstract can be omitted, can also write only part, such as public void f ()), can not be private, protected and other types.

2. The properties of the interface can only be public static final.

    • Why does it have to be public? Because the interface must be implemented to make sense, the variables and methods of the interface must be public.
    • Why does it have to be static? A static decoration is a class that is represented by the load of the class, and if it is non-static, it represents the object, and only when the object is created, and the interface is not an object.
    • Why does it have to be final? If the variable is not final, then each class that implements the interface can change the value of the variable, and it violates the OCP principle (the OCP principle: open for extensions and closed for changes).

3. Why can abstract classes not inherit more?

If there is a method F () in A, B and C inherit the a,d inherit B and C, that a A = new D (), A.F () will call the method of B or C method? This is the fatal block problem.

4. Why isn't there a fatal block problem with the interface?

If interface A has method F (), interface B and interface C inherit a,d B and C, that a = new D (), A.F () calls the method of D because both B and C are abstract methods.

5. Interface and the same point of the abstract class.

    • is not initialized.
    • All can achieve polymorphic

6. Interfaces and different points of abstract classes.

    • Interface methods are public abstract, member variables are public static final, abstract class is arbitrary.
    • Interface Like-a, abstract class Is-a
    • Interface Multiple Inheritance
    • Interfaces are more abstract than abstract classes
    • Abstract class has a construction method
    • Abstract classes can have methods that are implemented.

5. Application scenarios for interfaces and abstract classes.

    • When the focus is on the nature of things, use abstract classes. When focusing on the operation, use the interface.
    • Is-a and Like-a
    • Abstract classes are much more powerful than interfaces, but because high-level languages (and in practical terms) each class can inherit only one class, the cost of defining an abstract class is high. In this class, you must inherit or write all the commonalities of all its subclasses. Although the interface is much weaker in functionality, it is only a description of an action. And you can implement multiple interfaces in one class at the same time. In the design phase will reduce the difficulty. Interfaces are also more suitable for reuse.
    • Because the interface is more concerned with the implementation of the action, polymorphism is mainly to separate "what" and "How to do", so another important application of the interface is the implementation of polymorphic (of course, the abstract class can also achieve polymorphism, but the interface is more appropriate). Why is it that the interface is more suitable for polymorphism? Because there are implementations between the parent and son of an abstract class, unlike what is "done" in the interface, the implementation class is "How to do", at a glance.

Java abstract classes and interfaces

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.