Implements is also a keyword that implements inheritance between a parent class and a subclass, such as Class A inherits class B, written as a 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 implements
Extends 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
A class declares itself to use one or more interfaces through 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 the interface: 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 attack, and some can shoot long distance
Let's say the game requires a few strange things--
Wild Dog: Moving on the ground, attacking at close range
Black bear: Ground movement, near/far distance attack
Vulture: Ground/Sky moving, long-range attack
Piranha: Moving in water, near-range attack
Crocodile: Moving in the ground/water, near-range attack
Obviously, if we define each monster as a class, it is not an object-oriented program development, we should use the interface:
Interface onearth{//Land Interface
int earthspeed;//Land Movement speed
void Earthmove ();//Land Movement method
}
Interface onwater{//Water Interface
int waterspeed;//moving speed in water
void Watermove ();//Water Moving method
}
Interface onair{//Air interface
int airspeed;//moving speed in water
void Airmove ();//Water Moving method
}
Interface nearattack{//near-range attack interface
int nearattackpower;//attack at close range
void Nearattack ();//short-range attack method
}
Interface farattack{//Long Range attack interface
int farattackpower;//Long Range attack
void Farattack ();//Long Range attack method
}
In this way, depending on the requirements, we can choose to inherit the interface:
Class Tyke implements Onearth, nearattack{//Dingo
void Earthmove () {//method to implement inheritance 1
}
void Nearattack () {//method to implement inheritance 2
}
}
Class Blackbear implements Onearth, Nearattack, farattack{//black bear category
void Earthmove () {//method to implement inheritance 1
}
void Nearattack () {//method to implement inheritance 2
}
void Farattack () {//method to implement inheritance 3
}
}
Class Vulture implements Onearth, OnAir, farattack{//vultures
void Earthmove () {//method to implement inheritance 1
}
void Airmove () {//method to implement inheritance 2
}
void Farattack () {//method to implement inheritance 3
}
}
Class Maneatfish implements Onwater, nearattack{//cannibal Fish
void Watermove () {//method to implement inheritance 1
}
void Nearattack () {//method to implement inheritance 2
}
}
Class Crocodile implements Onearth, Onwater, nearattack{//crocodile
void Earthmove () {//method to implement inheritance 1
}
void Watermove () {//method to implement inheritance 2
}
void Nearattack () {//method to implement inheritance 3
}
}
Java's implements