The use of extends and implements in Java

Source: Internet
Author: User

1. in the declaration of a class, create a subclass of a class with the keyword extends. A class declares itself to use one or more interfaces through the keyword implements.
Extends is inherited from a class, you can use the parent class method after inheritance, or you can override the parent class method; Implements is to implement multiple interfaces, the method of the interface is generally empty and must be rewritten to be used.

2.extends is inherited from the parent class, as long as the class is not declared final or the class is defined as abstract can inherit, Java does not support multiple inheritance, but can be implemented with interfaces, so it is necessary to use the implements, Inheritance can inherit only one class, but implements can implement multiple interfaces, separated by commas.

3. extends, you can implement a parent class, or you can call the parent class to initialize This.parent (). It also overrides the variables or functions defined by the parent class. The benefit is that the architect defines the interface so that the engineer can do it. The overall project development efficiency and development costs are greatly reduced. Instead of implements, implement the parent class, subclasses cannot overwrite the parent class's methods or variables. Even if the subclass defines a variable or function that is the same as the parent class, it is replaced by the parent class.

Summary: These two implementations of the specific use, is to see the actual situation of the project, need to implement, can not modify the implements, only the definition of the interface needs to be implemented, or can be modified to be good extensibility, with extends.

The use of extends and implements in Java

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.