21_ java object-oriented interfaces (interface)

1. Overview of the interface

When the method is an abstract method of the abstract class when the abstract class can use another form to define and represent that the interface interface. And an array of interfaces, classes, class is an abstract concept the same level.

Interface is better than "abstract class" but also "abstract" and "abstract class", it can be more standardized sub-class constraints. Comprehensive professional achieved: separation of specification and implementation specific.

Abstract class also provides some specific implementation, the interface does not provide any implementation interface all methods are abstract. The interface is completely oriented specification of a predetermined number of the class has public methods specification.

Interface from the implementation point of view, the interface defines the services that can be provided to the outside.

From the perspective of the caller interface, the interface defines the implementer to provide those services.

Interface Specification is a standard for communication between two modules, communication. If you want to be able to design a system between well-defined interfaces between modules, equivalent to the completion of the outline design of the system, the rest is the building blocks of concrete reality. They work in the future, when the system is often done using "interface-oriented" thinking to design the system.

When you define a transport, I define an abstract class Vehicle, defined subclass of Car, Plane, Train. no problem. Abstract classes and subclasses of a general and special relationship.

Interface, what he achieved and subclass relationship rules. For example: I define an interface Runnable, Car achieve it can run on the ground, Train realization can run on the ground. It can also achieve the aircraft on the ground running. That is, if he is a vehicle, we will be able to run, we must implement Runnable. I would define the interface: Fligntable, if you want to fly in the sky it must be achieved Flightable interface.

2. Definition Interface

Keywords are not class-defined interface, but with a modified interface.

[Example] defined interface

interface 接口名 [extends 父接口1, 父接口2,] { 
	// 常量定义
	// 方法定义
}

Still .java file defines the interface resides, although after the declaration is compiled using the interface keyword will still generate .class files. This allows us to interface contains only seen as a special class function declaration.

Note: Use multiple inheritance can extends the interface! Interfaces can not be instantiated!

Interface consists of:

Interface defined attributes: global static constants belong to, to modify the default public static final.

The method defined in the interface: in JDK1.8 before, are all abstract methods to modify the default public abstract,

After JDK1.8, the interface may also include ordinary static method and modified default member methods.

[Example composition of the Interface

public interface Flyable {
	// 属性:默认为全局静态属性
	/*public static final*/ double MAX_HEIGHT = 8000;
	// 方法:JDK1.8以前默认为抽象方法
	/*public abstract*/ void fly(); 
	// JDK1.8之后还可以包含静态方法和default修饰的成员方法
	public static void showInfo1() {}
	default void showInfo2() {}
}

Note: You can not have a constructor interface because the interface does not require the member variable initialization.

3. implement interface

Inheritance relationship extends between classes is to achieve the relationship between class and implements the interface.

Interface format:

class 实现类 [extends 父类] implements 接口 {
	// 重写接口中的方法
}

After the class implements the interface, the abstract class will be inherited interface method, then the class of the abstract method needs to be rewritten, to complete specific logic.

[Example] implements the interface

// 接口
interface Flyable {
	// 建议不要省略,以提高阅读性
	/*public abstract*/ void fly(); 
}
// 实现类
class Bird implements Flyable{
	public void fly() {
		System.out.println("鸟儿在天空自由的飞翔");
	}
}
// 测试类
public class Test {
	public static void main(String[] args) {
		Bird bird = new Bird();
		bird.fly();
	}
}

Precautions:

  1. Subclasses implement all abstract methods of the interface, then the sub-classes can be instantiated.

  2. If only partial subclass interface abstract method, then the subclass is an abstract class that can not be instantiated!

4. Multi-implemented interface

Understand the characteristics of the interface, you then think about why you want to define interfaces, use abstract classes describing is not a problem, dim the interface in the end it? Interface fully supports multi-realization, and similar class inheritance, implementation class can implement multiple interfaces, will get all the interfaces defined.

In java, a class can only inherit a class, but can implement multiple interfaces at the same time, both can achieve the effect of multiple inheritance and function, but also to avoid the risk of multiple inheritance.

[Example] to achieve more than the case of the interface

// 父类
class Person {
	String name;
	public void eat() {}
}
// 接口
interface Runner {
	public abstract void run();
}
interface Flyer {
	public abstract void fly();
}
// Student类,既继承Person类,又实现Runner和Flyer接口
class Student extends Person implements Runner, Flyer {
	@Override
	public void fly() {
		System.out.println("Student fly方法");
	}
	@Override
	public void run() {
		System.out.println("Student run方法");
	}
}

The difference between abstract classes and interfaces

Only understand the difference between interfaces and abstract classes, then you can know when the choice of interfaces, when the selection of an abstract class.

Interface is an abstract of the action, an abstract class is an abstract roots. Level of abstraction (high to low): Interface > abstract> General Category.

Interface: people can eat, the dog can eat, we can "eat" is defined as an interface, then let these categories to achieve it.

Abstract class: people have names and ages of attributes, but also eating behavior, dogs have names and ages of attributes, but also eating behavior, we can put "the same attributes and behavior" is defined in an abstract class, then let these classes to inherit this abstract class.

Same point:

  1. Are constantly drawn up comes, do not be instantiated.

  2. It contains abstract methods, but does not provide specific implementation.

difference:

  1. Abstract class needs to be inherited extends, and only single inheritance;

    The interface needs to be implemented implements, and can achieve more than.

  2. You can define an abstract class member variables can also define static variables;

    Variables defined in the interface, the default is global static constants (public static final modified).

  3. An abstract class can define constructors, and to facilitate sub-class calls the abstract class member variable assignment;

    Interface can not define a constructor method, because the interface does not need to initialize member variables!

  4. An abstract class can be defined in the conventional method, you can also define an abstract method (abstrct);

    Before Interface JDK1.8, you can only define abstract methods (public abstrct), after JDK1.8 can also define a static method.

  5. Inheritance, described "is a" relationship abstract class that defines the substantially common content of the system;

    Implementation of the interface, "is like a" relationship described in additional conduct for outside the defined system.

6. oriented programming interface

Oriented programming interface is part of the object-oriented programming.

Why oriented programming interface? Software design is the most difficult to handle complex changes in demand, changes in demand more reflected in the implementation. If the sea of ​​our programming to revolve around specific implementation will fall into a "complex change", the software will not be finally realized. We have to carry around some sort of stable things to inaction, achieving the standard of quality projects.

The interface is standardized, the project is the most stable thing! Oriented programming interface allows me to grasp the real core of things, the implementation of complex and varied needs becomes possible. By scalability and maintainability oriented and programming interfaces, not the implementation class oriented programming, can greatly reduce the coupling between program modules, improving the overall system.

The concept oriented programming interface is much greater than the interface concept itself. Design difficult, when you did not write it in order to achieve a good interface, the interface for a change went wrong, difficult to design than the realization! Interface syntax itself is very simple, but how to really use? He is asked university. We need to re-use later in the project, we can appreciate. Learned here, understand the basic concepts, familiar with the basic grammar, it is a good student.

ps: For the latest free documentation and instructional videos, please add QQ group (627,407,545) receive.

Published 55 original articles · won praise 0 · Views 787

Guess you like

Origin blog.csdn.net/zhoujunfeng121/article/details/104619464