What is the difference 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
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 ");
}
}

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.

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.