The Implements__java of Java

Source: Internet
Author: User
Implements is also a keyword that implements inheritance relationships between parent classes and subclasses, such as Class A inheriting class B written as Class A implements b{}.
Implements is a class that implements an interface 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 ();} However, the interface has no method body. The method body can only be implemented through a specific class. Such as Chinese this class, the implementation of the People interface. Public class Chinese implements people{public say () {System.out.println ("hello.") ");}}
note points for interface implementations:A. Implementing an interface is all the methods for implementing the interface (except for 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 by the parent class, as long as that class is not declared final or the class is defined as abstract, or it can invoke the parent class to initialize This.parent (). The variables or functions defined by the parent class are overwritten. The benefit is that the architect defines the interface so that the engineer can implement it.       The overall project development efficiency and development costs are greatly reduced. Multiple inheritance is not supported in Java, but it can be implemented using interfaces, which requires implements, inheritance can only inherit a class, but implements can implement multiple interfaces, separated by commas. Implements the parent class, which cannot overwrite methods or variables of the parent class. Even if the subclass defines the same variable or function 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 a keyword implements.  In the declaration of a class, you create a subclass of a class by using the keyword extends. Class subclass name extends parent class name Implenments interface Name {...
If the extends and implements are used at the same time, extends must precede the Implements keyword.

Example:
Here's a game, Tarzan. The protagonist is a separate class, where we mainly use monsters to illustrate the use of the interface: There are many kinds of monsters, by region: Some fly in the sky, some on the ground run, some in the water to swim according to the way of attack: some can be close to physical attack, some can shoot from distance
Suppose the game requires a few of these kinds of monsters-wild dogs: Moving on the ground, attacking the black bear at close range: Moving on the ground, near/far away from the vultures: moving on the ground/in the sky, attacking the piranha from a distance: moving in the water, attacking crocodiles close to the ground
Obviously, if we define each monster as a class, it is not object-oriented program development, we should use the interface: interface onearth{//land interface int earthspeed;//land moving speed void earthmove (); Land Move Method}
Interface onwater{//Water interface int waterspeed;//water moving speed void watermove ()//Water Move Method}
Interface onair{//Air interface int airspeed;//water moving speed void airmove ()//Water Move Method}
Interface nearattack{//near-range attack interface int nearattackpower;//close range attacking void nearattack ()/Close range attack method}
Interface farattack{//long distance attack interface int farattackpower;//long range attacking void farattack ()//Long Distance attack method}
Thus, depending on the requirements, we can select the inherited interface: Class Tyke implements Onearth, nearattack{//feral dog class void Earthmove () {//Implementation inherited method 1} void Nearattack ( {//Implementation inherited method 2}}
Class Blackbear implements Onearth, Nearattack, farattack{//black bear class void Earthmove () {//Implementing inherited Method 1} void Nearattack () {//Implementing inherited Method 2} void Farattack () {//Implementing inherited Method 3}}
Class Vulture implements Onearth, OnAir, farattack{//vulture class void Earthmove () {//Implement inherited Method 1} void Airmove () {//Implementation inherited method 2} VO ID Farattack () {//Implement inherited Method 3}}
Class Maneatfish implements Onwater, nearattack{//cannibal fish void Watermove () {//Implementing inherited Method 1} void Nearattack () {//Implementing inherited Method 2}}
Class Crocodile implements Onearth, Onwater, nearattack{//crocodile class void Earthmove () {//Implementation inherited method 1} void Watermove () {//Implementation inheritance Method 2 } void Nearattack () {//Implementing inherited Method 3}}

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.