The difference and connection between abstract class and interface

Source: Internet
Author: User

First, we need to clarify the relationship between inheritance and implementation in object-oriented, and inheritance (Extends) is a kind of association between subclasses and parent classes, which can inherit non-private methods in the parent class, and can override the abstract methods of the parent class. Implementation (Implements) is a relationship between an interface and an implementation class, which must implement all the methods in the interface, and the methods in the interface cannot be declared as private methods.

Between the interface and the implementation class is good understanding, perhaps everyone for the abstract class and ordinary parent class, abstract methods and common methods do not understand, the following is the abstract class and abstract method, the general class and the ordinary method of a simple comparison.

First of all, we want to use the inheritance of the relationship between the parent class and the subclass, when the subclass inherits the parent class, it involves whether the parent class is a normal class or an abstract class, the parent class method is a common method or an abstract method.

abstract methods and common methods

Abstract methods are methods that are modified by the keyword abstraction, and the abstract method of the parent class must be overridden by the quilt class. Methods that are not modified by the abstract are ordinary methods, and the normal methods of the parent class can be inherited by the quilt class in addition to the private ones.

abstract class and ordinary class

Abstract class is also an abstract class, only the class has an abstraction, the class must be defined as an abstract class, the abstract class must be inherited, the abstract method must be overridden, non-abstract methods can not be rewritten. The process of overriding abstract methods in abstract classes is the polymorphic implementation of our object-oriented approach. The class that is not modified by the abstract is the ordinary class, the ordinary class can be inherited or can not be inherited, the ordinary class once inherited to become the parent class, the parent class of non-private methods can be inherited by the quilt class.

abstract classes and Interfaces

We can see from the above introduction that only abstract methods in abstract classes must be overridden by the quilt class, whereas in an interface, the implementation class must implement all the methods of the interface. In other words, when all the methods in an abstract class are abstract methods, the interface and the abstract class can be replaced with each other. However, this is somewhat partial, in Java can only achieve multiple inheritance, that is, a subclass can only inherit from the only parent class, and an implementation class can implement multiple interfaces, Java is the use of interfaces to achieve multiple inheritance. Therefore, in the case of multiple inheritance, even the methods in the abstract class are all abstract methods and can not replace the role of interfaces.


The difference and connection between abstract class and 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.