Source Address: Http://bgutech.blog.163.com/blog/static/182611243201162742122112/implements is also a keyword that implements inheritance between a parent class and a subclass, such as Class A inherits class B written as Class A implements b{}. Implements is a class that implements an interface with a keyword that is used to implement an abstract method defined in an interface. For example: People is an interface, he has say this method. public Interface People () {public say ();} But the interface has no method body. The method body can be implemented only through a specific class. For example, Chinese this class, the implementation of people this interface. Public class Chinese implements people{public say () {System.out.println ("Hello! ");}}
the point of attention of the interface implementation:A. Implementing an interface is all of the methods to implement the interface (except abstract classes). B. The methods in the interface are abstract. C. Multiple unrelated classes can implement the same interface, and a class can implement multiple unrelated interfaces.
the difference between extends and implementsExtends is inherited from the parent class, as long as the class is not declared final or the class is defined as abstract and can be inherited, or the parent class may be called 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. Java does not support multiple inheritance, but can be implemented with interfaces, so it is necessary to use the implements, inheritance can only inherit a class, but implements can implement multiple interfaces, separated by commas on the line. Implements the parent class, and the subclass 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.
format:Class A extends B implements C,d,e declares itself to use one or more interfaces by means of the keyword implements. In the declaration of a class, a subclass of a class is created with the keyword extends. Class subclass name extends parent class name Implenments interface name {...} If both extends and implements are used, extends must be placed before the Implements keyword.
Example: Here's a game for Tarzan. The protagonist is a separate class, here we mainly use monsters to illustrate the use of interfaces: monsters have many kinds, according to geographical points: some in the sky, some on the ground to run, some in the water to swim according to the attack way points: some can be close to physical attacks, and some can be long-distance shooting assume that the game needs such a few -- : Ground move, close attack black bear: Ground move, near/far attack Vulture: ground/Sky Move, distance attack Piranha: Water move, close attack alligator: ground/Water move, close attack Obviously, if we define each monster as a class, it's not an object-oriented program development, we should use interfaces: interface onearth{//Terrestrial Interface int earthspeed;// Land Movement Speed void earthmove ();//Land Movement Method } interface onwater{//Water interface int waterspeed;//water movement speed void watermove ();//Water moving method } interface onair{//Air interface int airspeed;//water movement speed void Airmove ()///Water Moving method } interface nearattack{//Proximity attack interface int nearattackpower;//melee attack void Nearattack ()////Proximity attack } interface farattack{//remote attack interface int farattackpower;//long range damage void Farattack ()///Remote attack method } Thus, depending on the requirements, we can selectively inherit the interface: class Tyke implements Onearth, Nearattack {//Wild Dog Class void Earthmove () {//Implement inherited Methods 1 } void Nearattack () {//Implement inherited methods 2 } } class Blackbear implements Onearth, Nearattack, farattack{//black Bear Class void Earthmove () {//method of implementing inheritance 1 } void Nearattack () {//Implement inherited Methods 2 } void Farattack () {//Implement inherited methods 3 } } class Vulture Implements Onearth, OnAir, farattack{//Vulture class void Earthmove () {//Implement inherited Methods 1 } void Airmove () {//method of implementing Inheritance 2 } void Farattack () {//Implement inherited methods 3 } } class Maneatfish implements Onwater, NearAttack{/ /Cannibal Fish void watermove () {//Implement inherited Methods 1 } void Nearattack () {//Implement inherited methods 2 } } class Crocodile implements Onearth, Onwater, nearattack{//crocodile class void Earthmove () {//Implement inherited methods 1 } void Watermove ( {//Implement inherited Methods 2 } void Nearattack () {//Implement inherited methods 3 } }
Java implements (go)