JAVA 14th (interface: implements and its basic applications), javaimplements

Source: Internet
Author: User
Tags define abstract

JAVA 14th (interface: implements and its basic applications), javaimplements
Interface:

We know that abstract classes can define abstract methods or non-Abstract METHODS. When methods in an abstract class are all abstract methods, we can define another manifestation: interface, so the interface is a special abstract class

The interface is represented by "Multi-inheritance" in another form, that is, "Multi-Implementation ".

Note: The interface definition is not a class, but an interface. Of course, it is still a class file during the final compilation.

interface Demo{abstract void show();abstract void show1();}

Common members in the interface: (all are fixed modifiers)

1. Global constant (public static final)
2. abstract method (public abstract)
3. Of course there are other

Interface Demo {public static final int num = 4; // the lifetime of num is 4 public int num = 4; // if this parameter is left blank, it is automatically added. By default, it is best to write it, otherwise, the readability is poor. public abstract void show ();}

It is concluded that the Members in the interface are public, and the-> permission is the highest.

If it is a class, it will use inheritance. If it is an interface, it will not be inherited. It is also a more intuitive expression, called implementation ).


Why is implementation?

The methods in the interface are abstract, so it is necessary to fully implement them. For abstract classes, some of them are not abstract and can be directly used by subclass. Inheritance can be used to get something that I don't need to do and can be used directly. The implementation is all I need to use and must be overwritten by myself.


Therefore, the Class and Class are inherited, and the class and interface are implemented.


Interface implementation features:

Import java. util. extends; interface Demo {public abstract void show (); public abstract void show1 (); public static final int num = 4;} // The Class and Class are inherited, the class and the interface are implementation relations (implements)/** the interface cannot be instantiated * It can only be a subclass that implements the interface, it can only be instantiated after it overwrites all the abstract methods in the interface * otherwise, this subclass is only an abstract class **/class Demo1_Imple implements Demo // after a class implements the interface, the implements interface {public void show () {System. out. println ("show print" + num);} public void show1 () {System. out. println ("show1 print" + num) ;}} public class Main {public static void main (String [] args) {variables in = new variables (System. in); demow.imple BLF = new demow.imple (); BLF. show (); BLF. show1 (); BLF. num = 5; // error. num is the final-modified System. out. println (BLF. num); // use numSystem for the object. out. println (Demo. num); // The static member System of the interface. out. println (demow.imple.num); // The static member num of the subclass that implements the interface }}

Interface advantages:

Java does not directly support multi-inheritance because of the call uncertainty (hidden risks). Therefore, java has improved the multi-Inheritance Mechanism of C ++, changed to multiple implementations in java (one class implements multiple interfaces)

Multi-inheritance: one or two parent classes are inherited. One parent class has two functions: A and B. The other parent class has two functions: C and D. Each inheritance has, four functions: B, C, and D
Multiple implementations: A parent class has two Abstract Functions: A and B. A parent class has two Abstract Functions: C and D. A subclass can implement these four functions.
(Small difference from multi-inheritance)
We pay attention to the functions of things. As for how to use the functions, it is determined by the subclass that if the features of the parent class already meet the requirements of the subclass, The subclass can be used directly. If not, the subclass still has this function, but it needs to be rewritten.

Why is the multi-Implementation of java called improved multi-inheritance?

The disadvantage of Multi-inheritance is that if the method names of the two parent classes are the same, there will be uncertainty. What about multi-implementation? It does not exist, because the show () method body is written by the subclass itself, but is inherited from the method body. After java is improved, there is no method body, the method body is determined by the subclass.

Multiple implementations and small exercises: interface A {public abstract void show ();} interface B {public abstract void show (); public abstract int add (int a, int B );} class C implements A, B {public void show () {System. out. println ("hello");} public int add (int a, int B) {return a + B + 10 ;}} public class Main {public static void main (String [] args) {c blf = new C (); BLF. show (); System. out. println (BLF. add (1, 3 ));}}

Therefore, multi-implementation improves multi-inheritance.

Of course, the following two test code methods cannot be tolerated by java

Interface A {public abstract void show ();} interface B {public abstract int show ();} Test 1: class C implements A, B {public void show () // If the C written in this way is still an abstract class, the {System. out. println ("hello") ;}} Test 2: class C implements A, B {public void show () {System. out. println ("hello");} public int show () {System. out. println ("Hello, world"); // show has been defined in C. Call show. The returned value cannot be determined. int or void }}

Of course, the interface function is far more

One class is inherited, And the other class is implemented multiple times (that is, a parent class is first created for the class, and then these functions are extended)

See the Code:

Interface A {public abstract void show ();} interface B {public abstract void show (); public abstract int add (int a, int B );} class D {public void print () {System. out. println ("") ;}// very fierce !!! Class E extends D implements A, B // E inherits D, implements A and B, in this way, E functions are enriched. {// E single inherits the print function in D, but I still want to use the functions in A and B, but these functions are not inherited, // implement the interface at the same time to extend the public void show () {System. out. println ("hello");} public int add (int a, int B) {return a + B + 10;} public void test () {System. out. println ("test") ;}}/** interface, avoiding single inheritance limitations */public class Main {public static void main (String [] args) {e blf = new E (); BLF. show (); System. out. println (BLF. add (1, 3); BLF. test (); BLF. print ();}}

There is an inheritance relationship between classes, an implementation relationship between classes and interfaces, and between interfaces?

The reason why java does not support multi-inheritance lies in the method body.


Interface A {public abstract void show ();} interface B {public abstract void show (); public abstract int add (int a, int B);} interface E extends, B // The inheritance relationship, and can be inherited more, more fierce !!! {// Multi-inheritance, the difference is on the method body, and the interface does not have a method body, so you can inherit public abstract void test ();} class G implements E {public void show () {System. out. println ("hello");} public int add (int a, int B) {return 250;} public void test () {System. out. println ("") ;}} public class Main {public static void main (String [] args) {g blf = new G (); System. out. println (BLF. add (3, 4); BLF. show ();}}


You can only say that java is really amazing !!!
The above is the basic implementation of the interface...


Interface features:

Interfaces are externally exposed rules.
An interface is a function extension of a program.
Interface appears to reduce Coupling
Interface can be used for multiple implementations
Classes and interfaces are implementation relationships, and classes can inherit a class to implement multiple interfaces at the same time.
Interfaces and interfaces are inherited

The interface concept is very big. Anything exposed to the outside is an interface, such as a USB Plug-in of a computer, to connect the mouse, the rule is that only the USB mouse can be connected.
Coupling: it can be understood to be closely related. Reducing coupling is to reduce the degree of closeness between the two. For example, early laptops, computers, and mice were connected, which was very inconvenient, the emergence of the USB interface allows many different mouse to connect to the computer, as long as the mouse is USB (that is, in line with the USB rule ), this reduces the coupling between the computer and the mouse. At the same time, USB can also be inserted into the USB disk, which reflects the function expansion.


Differences between interfaces and abstract classes:


Commonalities: they are all abstract concepts that are constantly extracted.
Differences:

1. abstract classes reflect the inheritance relationship. A class can only but inherit
An interface is an implementation relationship. One interface can be implemented multiple times.

2. abstract classes are inherited and "A is a B" (A is B). They define the basic commonalities of the system (I am A sophomore this year and I am a sophomore)
The interface is implemented. It is "A like a B", and A is similar to B. The additional functions of the definition system (the basic function of sophomore students is to learn, some will smoke, some will not, smoking is a system of sophomore students. Smoking is an additional function)

3. abstract classes can define abstract and non-Abstract methods for subclass to directly use them.
The interface must be fully abstract, its method subclass must be implemented by itself, and its members must also be fixed modifiers.

Abstract dog {public static vois is ();} the interface blind guide // The Blind guide method may be different. The blind Guide is an additional function of the dog, so the interface {// Why does the blind guide do not define classes? It cannot be inherited. // Why is the dog not defined as an interface? // Some dogs may have the same commonalities, and all dogs are the same. Therefore, it is unnecessary to define these commonalities // as an abstract method, it can be defined as a general method, and these interfaces cannot be implemented as public static void blind Guide ();} class guide dog extends dog implements guide blindness // here you can even implement {public void () {} public void guide blindness () // guide blindness, not necessarily dogs, pigs, cats, after training, you can also guide the blind. {// then, the blind guide is a common content-> extraction // you may think too much about each category (Guide Dogs and Cats. Pig). Why is this method used to guide the blind? // Think like this: on a guide dog, on a guide cat, or on a guide pig, it is better to have the guide function. // the answer is self-evident }}

Therefore, there are different analysis methods in different problem areas, so do not solidify the way of thinking.

Interface application:

/*
* Laptop usage
* To expand the functions of a laptop, I do not know how to use the functions.
* Therefore, to define a rule, as long as the rule is met, the notebook can have this function.
* This rule is the USB interface. The mouse, keyboard, and video can be used only when the USB rules are met.
* This rule is interface in java */


Interface USB // exposed rules {public abstract void open (); // enable public abstract void close (); // close} class Mouse implements USB // implementation rule {public void open () {System. out. println ("Mouse open");} public void close () {System. out. println ("Mouse close") ;}} class Upan implements USB {public void open () {System. out. println ("Upan open");} public void close () {System. out. println ("Upan close") ;}// the coupling between these devices and the computer reduces the public class Main {public static void main (String [] args) {UseUsb (new Upan (); // feature extended UseUsb (new Mouse ();} // use the rule public static void UseUsb (USB a) // interface reference, the object of the subclass used to accept the interface {. open ();. close ();}}



Implements usage in JAVA

Implements
Interface Sport
{
Void run ();
Void jump ();
}
Class Athlete implements Sport
{
Public void run ()
{
System. out. println ("sprint ");
}
Public void jump ()
{
System. out. println ("High Jump ");
}
Public static void main (String [] args)
{
Ahtlete al = new Ahtlete ();
Al. run ();
Al. jump ();
}
}

In Java, what is the difference between implements and extends When interfaces inherit interfaces?

Interfaces can inherit from other interfaces but cannot implement other interfaces.
That is to say, you can write the following:
Public interface secondInterface extends FirstInterface
However, you cannot write this statement:
Public interface Collection implements Iterable <T>
An interface cannot be implemented. Only a class can implement the interface.

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.