How to Use extends and implements in Java

Source: Internet
Author: User

I am confused by the Java language, the extends and implements in the Code. Now I can understand the differences and usage between them.

// Define a runner interface <br/> Public inerface runner <br/>{< br/> int id = 1; <br/> void run (); <br/>}

 

// Define an interface animal that inherits from the parent class runner <br/> interface animal extends runner <br/>{< br/> void breathe (); <br/>}

// Defines the fish class, which implements the methods run () and breather () of the animal interface () <br/> class fish implements animal <br/> {<br/> Public void run () // The animal method run () is implemented () <br/>{< br/> system. out. println ("fish is refreshing Ming"); <br/>}< br/> Public void breather () <br/>{< br/> system. out. println ("fish is bubbing"); <br/>}< br/> // defines an abstract class landanimal, which implements the animal interface method. <Br/> abstract landanimal implements animal <br/>{</P> <p> Public void breather () <br/>{< br/> system. out. println ("landanimal is breathing"); <br/>}< br/> // defines a class student, which inherits the class person, and implements the runner interface method run (). <Br/> class student extends person implements runner <br/> {<br/> ...... <br/> Public void run () <br/>{< br/> system. out. println ("the student is running"); <br/>}< br/> ...... <br/>}</P> <p> // defines an interface flyer <br/> interface flyer <br/>{< br/> void fly (); <br/>}</P> <p> // defines a bird class, which implements the methods defined by the runner and flyer interfaces. <Br/> class bird implements runner, flyer <br/>{< br/> Public void run () // method defined by the runner interface. <Br/>{< br/> system. out. println ("the bird is running"); <br/>}< br/> Public void fly () // method defined by the flyer interface. <Br/>{< br/> system. out. println ("the bird is flying "); <br/>}</P> <p> // testfish class <br/> class testfish <br/>{< br/> Public static void main (string ARGs []) <br/>{< br/> fish F = new fish (); <br/> Int J = 0; <br/> J = runner. ID; <br/> J = f. ID; <br/>}< br/>}

 

Note:

A) to implement an interface is to implement all the methods of this interface (except for abstract classes ).
B) methods in the interface 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
It 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 way, implements can be used to inherit only one class, but implements can implement multiple interfaces and use commas to separate them.


For example:




Class A extends B implements C, D, E {} (class subclass name extends parent class name implenments interface name)

 

The inheritance relationships between parent classes and child classes are different:

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, and A. J, A. G () is invalid. 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.

**************************************** **************************************** ***************

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.