The difference between an abstract class and an interface

Source: Internet
Author: User

Interface

1 because Java does not support multiple inheritance, there is an interface, a class can inherit only one parent class, but can implement multiple interfaces, the interface itself can inherit multiple interfaces.

The member variables in the 2 interface are public static final type by default. The initialization that must be displayed.

The method in the 3 interface is the public abstract type by default. An implicit declaration.

The 4 interface has no constructor method and cannot be instantiated.

The 5 interface cannot implement another interface, but it can inherit multiple interfaces.

Class 6 If you implement an interface, you must implement all the abstract methods inside the interface, otherwise the class will be defined as an abstract class.

Abstract class

1 If you declare a class as abstract, this class cannot generate objects and can only be used by inheritance.

2 The abstract method must exist in an abstract class.

3 There can be general variables and general methods in the abstract class.

4 Subclass inheriting an abstract class must implement an abstract method, unless the subclass is an abstract class.
private void Print () {}; This statement represents a null implementation of the method.
abstract void print (); This statement represents the abstraction of the method, without implementation.

Java provides and supports the creation of abstract classes and interfaces. Their implementations have something in common, and the difference is that:

    • All methods in an interface are implicitly abstract. Abstract classes can contain both abstract and non-abstract methods.
    • A class can implement many interfaces, but only one abstract class is inherited
    • Class if you want to implement an interface, it must implement all the methods that the interface declares. However, a class can not implement all methods of an abstract class declaration, and of course, in this case, the class must also be declared abstract.
    • An abstract class can implement an interface without providing an interface method implementation.
    • The variables declared in the Java interface are final by default. An abstract class can contain non-final variables.
    • The member functions in the Java interface are public by default. The member functions of an abstract class can be private,protected or public.
    • An interface is absolutely abstract and cannot be instantiated. An abstract class can also not be instantiated, but it can be called if it contains the main method.

The difference between an abstract class and an interface

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.