Java interface

The concept of interface in Java, an interface is a collection of all abstract methods. An interface needs to be defined with interface. There can only be abstract methods and constants.

The interface embodies the function of thing extension. In Java, a class defines an entity, including its attributes and behavior. The interface defines the possible actions of an entity, with only one declaration and no specific behavior.

When a method has different embodiments in many classes, it can be abstracted into an interface.

There can only be unmodifiable global constants and abstract methods in the interface. The interface has no constructor.

Define an interface:

Interface {public static final int i = 10; / / global constant prefix public static final public abstract void eat(); / / abstract method} interface features interface definitions are used when defining interfaces. Different from abstract classes, class is not required. Interfaces cannot be instantiated and objects cannot be created directly, because there are only abstract methods and no specific functions in interfaces. Interfaces can inherit interfaces. To realize specific functions, an interface must have subclasses that implement it. All abstract methods of the interface must be rewritten in the subclasses that implement the interface. Multiple interfaces can be inherited between interfaces. Subclasses of interfaces can be abstract classes, but they have no practical significance. A subclass can implement multiple interfaces through the implements keyword. Interfaces need to be polymorphic to create objects. As follows:

Interface a {public static final int i = 10; / / the interface can only define global constants. You need to add public static final. If not, it is recommended. Public abstract void eat();} Interface B extends a {/ / the interface can inherit the interface public abstract void sleep();} interface C { public abstract void look();} Abstract class D {/ / define an abstract class public void run() {system.out.println ("run");} public abstract void learn();} Public class text extensions D implements B, C {/ / a subclass can inherit an abstract class and implement multiple interfaces at the same time. / / if the subclass is not an abstract class, it must implement all abstract methods public void eat() {/ / implement the abstract method system.out.println ("eat") in interface a;} Public void look() {/ / implement the abstract method system.out.println ("view the scenery") in interface C;} Public void sleep() {/ / implements the abstract method system. Out. Println ("sleep in");}@ Override public void learn() {/ / override the abstract method system.out.println ("learn java") in abstract class D;} Public static void main (string [] args) {B B = new text(); / / polymorphic. The interface reference points to the subclass object b.eat(); b.sleep(); system. Out. Println (B.I); / / static constants are accessed through the interface name. It is not recommended to call C C = new text(); c.look(); d = new text(); d.learn();} / * ** The output is: eat, sleep and lie in 10. Look at the landscape science Java * / interface as the formal parameter. If the formal parameter of a method is the interface, it is as follows:

interface Inter{ public abstract void learn();} Class B {public void student (inter a) {/ / the formal parameter is interface a.learn();}} Public class text {public static void main (string [] args) {}} what do we do when we want to call the student method in class B? We have two ways

Because the interface cannot create an object, we need to give its sub implementation class and create the object of the sub implementation class to call its methods. It can be implemented by internal classes without providing subclasses. How to create subclasses:

interface Inter{ public abstract void learn();} class B{ public void student(Inter a) { a.learn(); }} Class interimpl implements inter {/ / create a sub implementation class of Inter, public void learn() {system. Out. Println ("learning");}} Public class text {public static void main (string [] args) {inter I = new interimpl(); / / create the interface object B B = new b(); / / create the class B object b.student (I); / / pass the object to the student method to call the learn method} }/*** the output is: learning * / inner class. If a class is defined in another class or method, this class is called inner class. Inner classes include member inner class, local inner class, anonymous inner class and static inner class.

Member inner class the member inner class is in the member position of the external class. The member inner class can unconditionally access all the contents of the external class, including static and private.

Public class outside {int a = 10; private int b = 20; class inside {/ / inside is the member's internal class public void show() {/ / the methods of the internal class system. Out. Println (a); system. Out. Println (b);}}} be careful:

When the internal class defines the same member variables and methods as the external class, the proximity principle gives priority to accessing the variables and methods of the internal class. If you need to use the members and methods of the external class, you need to use the this keyword

External class this. Member variable external class this. The external class of the member method cannot directly access the variables and methods of the internal class. It needs to be accessed by creating the object of the internal class:

Public class outside {int a = 10; private int b = 20; class inside {/ / inside is the internal class of the member. Public void show() {/ / the methods of the internal class system. Out. Println (a); system. Out. Println (b);}} public void show2() { Inside i=new Inside(); i.show(); }} The internal class of a member is attached to an external class, that is, if you want to create an object of an internal class, you need an object of an external class, and the methods or variables in other classes that need to access the internal class of the member need to pass:

External class name Internal class name object name = external class object Inner class object

To create an object of a member's inner class:

Class outside {int a = 10; private int b = 20; class inside {/ / inside is the member's internal class public void show() {/ / the methods of the internal class system. Out. Println (a); system. Out. Println (b);}}} Public class text2 {public static void main (string [] args) {outside. Inside I = new outside(). New inside(); / / format of the object creating the inner class i.show();}}/** The output is: 1020 * / if the access permission of the member's internal class is private, this internal class can only be accessed in the external class, and objects of private internal classes cannot be created in other classes through the above methods.

Class outside {int a = 10; private class inside {/ / a private member inner class that can only be used in external classes. Public void show() {/ / the method of the inner class system. Out. Println (a);}}} Public class text2 {public static void main (string [] args) {/ / outside. Inside I = new outside(). New inside(); / / i.show(); / / error, unable to access private inner class}}

Local inner class local inner class is a class defined in a method. The access of local inner class is limited to the method or the scope.

When a local internal class accesses a local variable, it needs to add final to the local variable

Because local variables are variables defined in the method, when the current method is executed, the local variables will be recycled by Java. At this time, if the object of the local internal class still references the local variables, the object will point to a non-existent variable. Therefore, the local internal class can only access the local variables modified by final.

Class outside {public void show() {final int a = 1; class inside {public void show2() {system. Out. Println (a); / / when a local internal class accesses a local variable, it needs to add final to the local variable. / / after JDK7, if it is not added, it will be automatically given, but it will warn}}} the local internal class cannot have modifiers such as public, private and static. In the static internal class format, the internal class is preceded by static. Its characteristic is that it can only access the members modified by static in the external class.

Class outside {static int a = 10; static class inside {/ / static inner class public void show() {system. Out. Println (a); / / only external static members can be accessed}}} public class text2 {public static void main (string [] args) {outside.inside I = new outside. Inside(); / / static inner class creates objects i.show();} / * ** The output is: 10 * / anonymous inner class anonymous inner class is most used in actual development. The premise of anonymous inner class is that there is a class or interface, which can be an abstract class.

The format of anonymous inner class is:

New class name or interface name {overridden method;} An anonymous inner class essentially inherits the class or implements the methods in the interface.

For example, if the formal parameter mentioned above is an interface, you can rewrite the abstract methods in the interface by anonymous internal classes without creating subclasses.

interface Inter { public abstract void learn();} class B { public void student(Inter a) { a.learn(); }} Public class text {public static void main (string [] args) {inter a = new inter() {/ / anonymous inner class public void learn() {/ / directly rewrite the abstract methods in the interface in the anonymous inner class, without creating the sub implementation class system.out.println ("learning");}}; a.learn(); // Call the learn method} / * * and the output is: learning * / example analysis: complement the code interface interface {void show();} Class outer {/ / supplement code} class outerdemo {public static void main (string [] args) {outer. Method(). Show();}} It is required to first look at the main method in the problem of "HelloWorld" output on the console, and outer The sentence method () holds. It means that the method method is static. Only static methods can be called directly by the class name. Keep looking

Outer. method(). show(); This statement holds, indicating that the method method returns an inter anonymous object, and only the object can pass through Method name to call the method.

Because there is no sub implementation class of the inter interface, we need to return an anonymous inner class that implements the inter interface and implement the abstract method of the interface in the anonymous inner class.

Therefore, the supplemented code is:

interface Inter { void show();} Class outer {/ / supplement code public static inter method() {return new inter() {/ / returns the anonymous inner class that implements the interface. Public void show() {/ implement the show method system.out.println ("Hello word");}};}} class OuterDemo { public static void main(String[] args) { Outer.method().show(); }}/** Hello word * * / formal parameter is a problem in abstract class abstract class a {public void sleep() {system.out.println ("sleep");}} class B{ public void cat(A a) { a.sleep(); }} Public class text {public static void main (string [] args) {}} if we need to call the cat method in class B, because class A is an abstract class and cannot directly create class a objects, we need to create a subclass for class a first, and then create class a objects in a polymorphic way. Pass in the formal parameters of the cat method.

Abstract class a {public void sleep() {system.out.println ("sleep");}} class B{ public void cat(A a) { a.sleep(); }} class C extends A{ }public class Text { public static void main(String[] args) { A a=new C(); a.sleep(); }}/** * The output is: sleep * / you can also use anonymous inner classes. The advantage is that you don't need to give additional subclasses of class A

Abstract class a {public void sleep() {system.out.println ("sleep");}} class B{ public void cat(A a) { a.sleep(); }} Public class text {public static void main (string [] args) {a a = new a() {/ / anonymous inner class. Because the sleep method is not an abstract method, it does not need to be overridden.}; a.sleep(); }}/** * Output: sleep*/

When to use interfaces and abstract classes

Abstract class is the abstraction of things themselves. For example, teachers and students can be abstracted as human beings.

Interface is the abstraction of behavior. For example, teachers should learn English and students should learn English. Learning English can be abstracted as an interface.

Abstract classes represent what the object is and interfaces represent what the object can do.

Therefore, abstract classes can only have one subclass. For example, teachers and students can only be human, not non-human.

Teachers or students can implement multiple interfaces. They can learn both English and mathematics. Original text: https://blog.csdn.net/liuchonghua/article/details/80013079

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>