Summary The Java interface modifiers

Java Interface
Interface (English: Interface), is in the JAVA programming language, an abstract type, is a collection of abstract methods, the interface is usually interface to declare. Interfaces inherit a class manner, thereby inherit the abstract interface methods.
Interface classes are not the way to write interfaces and classes are similar, but they belong to different concepts. Class attributes and methods described objects. Interface to be implemented for class contains.
Unless the implementation of the interface class is an abstract class, all methods of the class or interface to be defined.
The interface can not be instantiated, but can be implemented. A class that implements the interface, all the methods described within the interface must be implemented, otherwise it must be declared as an abstract class.
In Java, interface type can be used to declare a variable, they can become a null pointer, or is bound to an object of this interface.
First, the definition of the interface

格式:
1.	[修饰符] interface 接口名 extends 父接口1,父接口2,......
2.	{
3.	    //零到多个静态常量定义...
4.	    //零到多个抽象方法定义....
5.	}

Modifiers public, abstract
interface has the following features:
• the interface is implicitly abstract, when declare an interface, do not use the abstract keyword.
• Each interface method is implicitly abstract, abstract keyword does not require the same declaration.
• interface methods are public.
• compiled automatically added when a method defined in the interface public abstract modifier
• Java interfaces in the member variables can only be a common public static final modification, and must initial value, you can not write public static final, will automatically compile time Add
the sample code:

1.	public interface InterfaceOne {
2.	    //编译时自动为接口里定义的成员变量增加public static final修饰符
3.	    int INT_A=11;
4.	    public final static int INT_B=11;
5.	
6.	    //编译时自动为接口里定义的方法添加public abstract修饰符
7.	    void sleep();
8.	    public abstract void running();
9.	    void test();
10.	}

Second, the implementation of an interface
when the class interface when implemented, all of the class interface to implement a method. Otherwise, the class must be declared as an abstract class.
Use implements keyword class that implements the interface. In the class declaration, Implements keyword placed behind the class declaration.
• Java classes can implement multiple interfaces

格式:
1.	[修饰符] class 类名 implements 接口1[, 接口2, 接口3..., ...]
2.	{
3.	}

Sample code:

1.	public class InterfaceOneImpl implements InterfaceOne{
2.	
3.	    @Override
4.	    public void sleep() {
5.	        System.out.println("休息");
6.	    }
7.	
8.	    @Override
9.	    public void running() {
10.	        System.out.println("运动");
11.	    }
12.	
13.	    @Override
14.	    public void test() {
15.	        // TODO Auto-generated method stub
16.	
17.	    }
18.	}

Third, the interface inherit
an interface can inherit an interface to another, similar comparison and inheritance between classes. Inherited interface extends keyword, inherited his father's son interface method interface. Multiple inheritance allows the interface
Sample code:

1.	public interface InterfaceThree extends InterfaceOne,InterfaceTwo{
2.	
3.	}

Abstract classes and interfaces contrast
abstract class interface
the default way to achieve it can have default way to achieve
implementation subclasses use extends keyword to inherit the abstract class. If a subclass is not an abstract kind of thing, it needs to provide an implementation of the abstract class abstract method of all statements.
Constructor abstract class constructor can
distinguish normal Java classes in addition can not be instantiated abstract class Java classes, and there is no difference between ordinary
access modifier abstract methods can pubic, protected these modifiers, and default
main method abstract methods the main method may have and we can run it
multiple inheritance can inherit a class abstract class and implement multiple interfaces
to add new methods if you add new methods to an abstract class, you can give it a default implementation. So now you do not need to change your code.
Modifiers summarize
four access control characters:
Note: Without write access specifier, is friendly (the default) modifier
to access the range of private friendly protected pubic
same class √ √ √ √
same package √ √ √
child class √ √
√ globally
class modifiers:
• public (access control characters), a class declared as public class, he can be accessed by any object, and a program of class must be public.
• friendly, the default modifier, only objects in the same package in order to use this class.
• abstract, the method of a class declared as abstract classes, unfulfilled need to provide a subclass methods.
• final, life will be a class for the final (ie, non-inherited classes), he said he could not be other class inheritance.
Member variable modifiers:
• public (public access specifier), specify the variable as public, he may be accessed by methods of any object.
• private (private access control characters) specifies that the variables are only allowed access to their own class methods, other methods of any class (including subclasses) are both not accessible.
• protected (Protected Access control characters) specify the variable can not be accessed by their classes and subclasses. In a subclass can override this variable.
• friendly, in the same class can access a package, other packages in the class can not access.
• final, final modifier, specify the value of this variable can not be changed.
• static (static modifier) the specified variable is shared by all objects that can be used for all instances of the variable. Variables belong to this category.
Method modifiers:
• public (public control characters), specify the method can be accessed from all classes
specified • private (private control characters) This method can only have their own class and other access methods, other classes do not have access (including sub-categories)
• protected (protected access specifier) specifies the method may be for access to its classes and subclasses.
• final, specify the method can not be overridden.
• static, do not need to specify a method can be activated instantiated.
• synchronize, synchronize modifier, in multiple threads, the modifier is used before the operation, a method he belongs locked to prevent access to other threads, after the end of the run unlocked.
• native, local modifier. This method specifies the method body is in a language other external programs written.
Initialization block:
• static (optional), using the modified static initialization block is called a static block of code

Guess you like

Origin blog.csdn.net/weixin_42530700/article/details/89971508