Differences between extends and implements in Java

Source: Internet
Author: User

1. In the class declaration, use the keyword extends to create a subclass of the class. A class declares its own use of one or more interfaces through the implements keyword.
Extends inherits a class. After inheritance, you can use the method of the parent class or override the method of the parent class. implements multiple interfaces and the interface method is generally empty, must be rewritten before use
2. extends is an inherited parent class. As long as the class is not declared as final or defined as abstract, it can be inherited. Java does not support multiple inheritance, but can be implemented using interfaces, in this case, implements must be used. inheritance can only inherit one class, but implements can implement multiple interfaces and use commas to separate them.
For example

 
ClassAExtendsBImplementsC, D, E

I learned for a long time,Today I finally understandImplements,It's actually very simple.,Let's look at the following examples.OKLa~~
Interface concepts

 Public  Inerface runner {  Int Id = 1;  Void  Run ();}  Interface Animal Extends  Runner {  Void  Breathe ();}  Class Fish Implements  Animal {  Public   Void  Run () {system. Out. println ( "Fish is swimming Ming" );} Public   Void  Breather () {system. Out. println ( "Fish is bubbing" );}}  Abstract Landanimal Implements  Animal {  Public   Void  Breather () {system. Out. println ( "Landanimal is breathing" );}}  Class Student Extends PersonImplements  Runner {......  Public   Void  Run () {system. Out. println ( "The student is running" );}......}  Interface  Flyer {  Void  Fly ();}  Class Bird Implements  Runner, flyer {  Public   Void Run () {system. Out. println ( "The bird is running" );}  Public   Void  Fly () {system. Out. println ( "The bird is flying" );}}  Class  Testfish {  Public   Static   Void  Main (string ARGs []) {fish F = New  Fish (); Int J = 0 ; J = Runner. ID; j = F. ID ;}} 

Note:

A. Implementing an interface is to implement all the methods of this interface (except for abstract classes ).
B. Methods in interfaces are abstract.
C. Multiple irrelevant classes can implement the same interface, and one class can implement multiple irrelevant interfaces.
========================================================== ==============================

ExtendsAndImplementsDifferent

Extends inherits the parent class, as long as the class is not declaredFinalOr the class is definedAbstractCan inherit,JavaMulti-inheritance is not supported, but can be implemented using interfaces.ImplementsOnly one class can be inherited,ImplementsMultiple Interfaces can be implemented. Just use commas to separate them.
For example

ClassAExtendsBImplementsC, D, E

A class uses keywordsImplementsDeclare that you use one or more interfaces. In the class declarationExtendsCreate a subclass of a class.

 
ClassSubclass nameExtendsParent class name implenments interface name {...}

========================================================== ==============================

A = new B (); as a result, A is a Class A instance and can only access the method in Class A. Then it is equal to a A = new (); what is the difference?
================================================= ================================< br> Class B extends a
after inheritance, some parent classes are usually defined.
A = new B ();
you can upload the file.
A is an instance of a parent class object, so it cannot access the new member or method defined by the subclass.
================================================= ==================================< br> if the definition is as follows:

Class A {
int I;
void F () {}< BR >}< br> Class B extends a {
Int J;
void F () {}// rewrite
void g () {}< BR >}< br> then:
B = new B ();
B is an instance of the subclass object, you can access not only your own attributes and methods, but also the attributes and methods of the parent class. B. I, B. J, B. F (), B. G () are valid. B. F () is F () in access B
A = new B ();
although a is a constructor of B, however, if you become an instance of the parent class object after upcast, you cannot access the attributes and methods of the subclass. A. I, A. F () is legal, while a. J, A. G () is not. Access. F () is to access F () in B ()
================================================= ==================================< br> A = new B (); this statement actually has three processes:
(1) A;
declaring a as a parent class object is just a reference, unallocated space
(2) B temp = new B ();
an instance of Class B objects is created using class B constructor, that is, the initialization
(3) A = (a) temp;
converts the subclass object temp into a non-parent class object and assigns it to A. This is the upload (upcast ), is safe.
after the above three processes, A is a Class A instance.
The subclass has more attributes and methods than the parent class. It is safe to discard the upload, while downcast is sometimes added, which is usually insecure.
================================================= ======================================< br>. F () corresponds to Class B's method F ()
after the constructor is called to create an instance, the corresponding method entry has been determined.
In this case, although a is uploaded as class A, the overwritten method F () is still B's method F (). That is to say, each object knows which method should be called.
A a1 = new B ();
A a2 = new C ();
both A1 and A2 are Class A objects, however, F () is different. This is the embodiment of polymorphism on the first floor.

This type of problem is clearly explained in Java programming ideas.

 

 

 

 
Implements is generally an implementation interface.
 
Extends is an inherited class.
 
 
 
The interface is generally not defined only by the method declaration,
 
Java specifically points out that the implementation interface is justified, because inheritance may feel that the parent class has implemented the method, while the interface does not implement its own method and only has declarations, that is, a method header does not have a method body. Therefore, you can understand that an interface is a subclass that implements its method declaration instead of inheriting its method.
 
However, the method of a general class can have a method body, so it is reasonable to inherit.
 
The imported package can use all classes that implement non-interfaces in it. You can decide whether to implement the interface. If you want to use the interface, you cannot call it if you do not implement it. Because the interface is a standard, it is a set of method declarations without method bodies. Let me give you an example: an interface can be compared to a protocol. For example, if I say a protocol is "murder", you can use a machete to implement this interface. As for how to kill a machete, of course, you can also use snatching to implement the killing interface, but you cannot use the killing interface to kill, because the killing interface is just a function description, a protocol, and how to do it, it also depends on its implementation class.
 
If an interface exists in a package, you can skip this step. This does not affect your use of other classes.

 

 

Implements

Implements is a key word used by a class to implement an interface. It is used to implement the abstract methods defined in the interface. For example, people is an interface with the say method. Public interface people () {public say ();} but the interface does not have a method body. Only one specific class can be used to implement the method body. For example, the Chinese class implements the people interface. Public class Chinese implements people {public say () {system. Out. println ("Hello! ");}}

In Java, implements indicates that the subclass inherits the parent class. For example, Class A inherits Class B and writes it as class A implements B {}

AndExtendsDifferent

Extends can implement the parent class, or call the parent class to initialize this. Parent (). It also overwrites the variables or functions defined by the parent class. The advantage is that the architect defines interfaces so that engineers can implement them. The development efficiency and cost of the entire project are greatly reduced.

Implements implements the parent class. Child classes cannot overwrite the methods or variables of the parent class. Even if the subclass defines the same variables or functions as the parent class, it will be replaced by the parent class.

The specific use of these two implementations depends on the actual situation of the project and needs to be implemented. implements cannot be modified. Only the specific implementation of the interface needs to be defined, or extends can be modified and used.

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.