Meanings and differences between extends and implements

Source: Internet
Author: User
Extends inheritance class; implements implementation interface. Classes and interfaces are different: Program While the interface has no program implementation, you can only predefine the method extends inheritance class.

 

Implements implementation interface:
Java also provides an inheritance mechanism, but also provides an interface concept. Because Java's inheritance mechanism can only provide a single inheritance (that is, only one type of parent class can be inherited), Java interfaces are used to replace multiple inheritance of C ++. An interface is an interface that specifies the two objects to be communicated and the specifications to be communicated. From the Java programming language perspective, the Java interface indicates: some functions or data members are required to share the ownership of other objects belonging to different categories. These functions and data members are defined in one interface, and then all Java objects of different categories are allowed. it can be used together. Java class can inherit only one parent class (with the extends keyword), but can have (or implement) Many interfaces (with the implements keyword ).

 

What is the difference between extends and implements?
For Class, extends is used to (single) inherit a class, while implements is used to implement
An interface ). The interface is introduced to partially provide the multi-inheritance function.
In the interface, you only need to declare the method header and leave the method body to the implemented class.
These implemented class instances can be treated as interface instances.
You can also declare an extends (Multi-inheritance) Relationship between interfaces.
Note that one interface can be extends with multiple other interfaces. Interface
An interface is a set of methods that an object knows when calling other objects. An object can have multiple interfaces (in fact, interfaces are a subset of all methods of the object). The type is a specific interface of the object. Different objects can have the same type, and an object can have multiple different types. An object can only be known to other objects through its interface. In a sense, an interface is in a very limited way. It will be "..." It is expressed as "one type that supports this interface ...". Interfaces are the key implementation inheritance and interface inheritance for gability.
Implementation inheritance (class inheritance): the implementation of an object is defined based on the implementation of another object. Interface inheritance (subtype): Describes when an object can be used to replace another object. The Inheritance Mechanism of C ++ refers to both class inheritance and interface inheritance. C ++ implements interface inheritance by inheriting pure virtual classes. Java has a separate language constructor for interface inheritance-Java interface. Java interface constructor is easier to express and implement the design that focuses on object interfaces.

interface advantages
advantage: the client does not have to know the specific class of the object to use. An object can be easily replaced by another object that implements the same interface. The connection between objects does not have to be hard bound (hardwire) to a specific class object, thus increasing flexibility. Loosens coupling ). Added the possibility of reuse. Increases the chance of (object) combination, because the contained object can be any class that implements a specified interface. Disadvantage: the complexity of the design increases slightly (the interface represents "... Like ..." (Likea) relationship, inheritance represents "... Yes ..." (ISA) relationship. The combination indicates "… Yes ..." (Hasa .)

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.