The difference between an interface and an abstract class in Java

Source: Internet
Author: User

The common characteristics of interfaces and abstract classes are as follows:

Neither the interface nor the abstract class can be instantiated, at the top of the inheritance tree, for implementation and inheritance by other classes.

Both interfaces and abstract classes can contain abstract methods, classes that implement interfaces, or classes that inherit abstract classes must implement these abstract methods.

difference: The main difference between the two is that they exist for different purposes, that is, the purpose of the design is different.

Interface as a window of interaction between the system and the outside world, the interface embodies a specification.

Abstract class, as the common parent of many subclasses in the system, is a kind of template design.

What we are most concerned about is the difference between the two usages:

(1) An interface can contain only abstract methods and default methods, and cannot implement common methods. Abstract classes can completely contain common methods.

The default method is as follows:

Public interface A {    default   void foo () {       System.out.println ("Calling A.foo ()");}    }  public class Clazz implements A {} Even if the Clazz class does not implement the Foo () method. The default implementation of the Foo () method is provided in interface A. This is the default method .

(2) The static method cannot be defined in the interface, and can be in an abstract class.

(3) Only static variables can be defined in an interface, and ordinary variables cannot be defined, and abstract classes can define static variables and can define ordinary variables.

(4) The interface does not contain a constructor, and an abstract class can contain a constructor. But instead of creating the object, let the child class call the constructor to complete the initialization of the abstract class.

(5) The interface cannot contain an initialization block, but an abstract class can have initialization blocks.

(6) class is a single inheritance, including abstract classes, but the implementation of the interface can be multiple, supplementing the Java single inheritance of the shortage!

The difference between an interface and an abstract class in Java

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.