What is the difference between extends and implements in Java?

Source: Internet
Author: User

What is the difference between extends and implements in Java?
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
Class A extends B implements C, D, E

========================================================== ==============================
Implements
After learning for a long time, I finally understood implements. It is actually very simple. Let's take a look at the following examples ~~
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 refreshing 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 person implements 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.
========================================================== ==============================
Differences between extends and implements

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
Class A extends B implements C, D, E

//
A class declares its own use of one or more interfaces through the implements keyword. In the class declaration, you can use the keyword extends to create a subclass of the class.
Class subclass name extends parent class name implenments Interface Name
{...

}

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

A A = new B (); as a result, A is a Class A instance and can only access methods in Class A. What is the difference between a and a A = new?
========================================================== ============================
Class B extends
After inheritance, some members or methods that the parent class does not have are usually defined.
A A = new B ();
This is acceptable for uploading.
A is an instance of a parent class object, so it cannot access the new member or method defined by the subclass.
========================================================== ============================
For example:
Class {
Int I;
Void F (){}
}
Class B extends {
Int J;
Void F () {}// rewrite
Void g (){}
}
Then:
B = new B ();
B is an instance of a subclass object. It can not only access its own attributes and methods, but also the attributes and methods of its parent class. B. I, B. J, B. F (), B. G () are valid. In this case, B. F () is F () in B ()
A A = new B ();
Although a is a constructor of B, after upcast, it becomes an instance of the parent class object and cannot access the attributes and methods of the subclass. A. I, A. F () is legal, while a. J, A. G () is not. In this case, access a. f () is to access F () in B ()
========================================================== ============================
A A = new B (); this statement actually has three processes:
(1);
Declare a as a parent class object as a reference and no space is allocated.
(2) B temp = new B ();
Creates an instance of Class B objects through class B constructor, that is, initialization.
(3) A = (a) temp;
It is safe to convert the temp sub-class object into a non-parent class object and assign it to a. This is the upload (upcast.
After the above three processes, a has completely become a Class A instance.
Subclass has more attributes and methods than its parent class. Therefore, it is safe to discard the upload. downcast is sometimes added, which is usually insecure.
========================================================== ==============================
A. 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 a class, 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, but their F () are 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 {}

Different from extends

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.