Java knows how much (40) the difference between an interface and an abstract class

Source: Internet
Author: User

A class is a template for an object, and an abstract class and interface can be seen as a template for a specific class.

Because, from a certain point of view, the interface is a special kind of abstract class, their origins are quite deep, there are a lot of similarities, so in choosing who to use the problem easily confused. We first analyze the same points they have.
    • represent the abstraction layer of a tree-like structure. When using reference variables, try to use the abstraction layer of the class structure to separate the definition and implementation of the method, which has the advantage of being loosely coupled to the code.
    • cannot be instantiated.
    • Can contain abstract methods. Abstract methods are used to describe what functions a system provides without having to be concerned with specific implementations.

The main differences between abstract classes and interfaces are explained below.

1) Abstract classes can provide implementations for some of the methods, avoid repeating them in subclasses, improve the reusability of the code, which is the advantage of the abstract class, and the interface can contain only abstract methods and cannot contain any implementations.
1  Public Abstract classa{2      Public Abstract voidmethod1 ();3      Public voidmethod2 () {4         //A method25     }6 }7  Public classBextendsa{8      Public voidmethod1 () {9         //B method1Ten     } One } A  Public classCextendsa{ -      Public voidmethod1 () { -         //C method1 the     } -}
Abstract Class A has two subclasses B, C, because there is a method METHOD2 implementation in a, sub-class B, C does not need to rewrite the Method2 method, we say that A is a subclass provides a common function, or a constraints the behavior of subclasses. METHOD2 is an example of reusable code. A does not define the implementation of METHOD1, that is, B, C can be based on their own characteristics to implement the Method1 method, which reflects the characteristics of loose coupling.

Then switch to the interface to see:
1  Public Interfacea{2      Public voidmethod1 ();3      Public voidmethod2 ();4 }5  Public classBImplementsa{6      Public voidmethod1 () {7         //B method18     }9      Public voidmethod2 () {Ten         //B method2 One     } A } -  Public classCImplementsa{ -      Public voidmethod1 () { the         //C method1 -     } -      Public voidmethod2 () { -         //C method2 +     } -}
Interface A cannot provide common functionality for implementing Class B, C, which means that a cannot constrain the behavior of B and C. B, C can freely play their own characteristics of the real method1 and Method2 method, interface A has no control ability.

2) A class can inherit only a direct parent class (possibly an abstract class), but a class may implement multiple interfaces, which is the advantage of the interface.
1 Interfacea{2      Public voidmethod2 ();3 }4 Interfaceb{5      Public voidmethod1 ();6 }7 classCImplementsa,b{8      Public voidmethod1 () {9         //C method1Ten     } One      Public voidmethod2 () { A         //C method2 -     } - } the //you can use C in such a flexible way, and C has the opportunity to expand and implement other interfaces -A a=NewC (); -B b=NewC (); - Abstract classa{ +      Public Abstract voidmethod1 (); - } + Abstract classBextendsa{ A      Public Abstract voidmethod2 (); at } - classCextendsb{ -      Public voidmethod1 () { -         //C method1 -     } -      Public voidmethod2 () { in         //C method2 -     } to}


For Class C, there will be no chance of inheriting other parent classes.

To sum up, interfaces and abstract classes each have advantages and disadvantages, in the interface and the choice of abstract class, must abide by such a principle:
    • The behavior model should always be defined by interfaces rather than abstract classes, so it is often preferable to use an interface as little as possible with abstract classes.
    • When choosing an abstract class, it is usually the case that you need to define the behavior of the subclass and provide generic functionality for the subclass as such.
Series Articles:

Java know how much (top)

Java know how much (interface) interface

Java knows how much (40) the difference between an interface and an abstract class

Java knows how much (40) the difference between an interface and an abstract class

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.