First, what is abstract method, abstract class
Abstract method:
1, the background (why should have abstract method): There are some methods, a birth is used to let others inherit the use of rewriting, oneself completely did not go to realize the necessary, only with the definition can be, so in Java specifically this method is called abstract method.
2, definition: Use the abstract modifier to declare a method, and this method is only declared, not implemented.
For example:
Public abstract test ();//Declaration only, no implementation, even "{}" is not written.
Abstract class:
1, background: Because the abstract method is defined in the class, the abstract method is only the declaration of the method is not implemented, that is incomplete, so Java will have the incomplete method of the class, called abstract class.
2, definition: Similar to the previous abstract method, Java will be born to let people inherit the class, called abstract class.
Note: 1. Abstract methods must be in abstract classes, but abstract classes can have no abstract methods.
2. The class in which the abstract method resides must be an abstract class.
Second, what is the interface
1, the background (why have interface): Java does not have multiple inheritance concept, but the real life of the object exists in this phenomenon, so there is an interface.
For example: "Cat" belongs to "animal", also belongs to "cat" but in Java does not have multiple inheritance to describe, so use interface to describe---->class cat implements animal,felidae{}
2, definition: interface is the abstract method and the definition of the constant value of the set (the interface can not have variables , The interface method is an abstract method ), it is a special kind of abstract class .
---> Remarks: (1) The attribute declared in the interface (member variable) must be defined as public static final xx xx, even if you write int a in the interface, also by default as int xx= public static final int Xx.
(2) The method in the interface can only be an abstract method, must be public , must be abstract , even if defined as void Methmod, also default as void Methmod = public abstract void Methmod
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Special Note: The above provisions are versions prior to JAVA8, but in versions after JAVA8, there can be instance methods (public static void) in the interface, and there can be default methods (public default).
Why to update this feature, specific explanation can refer to: http://www.cnblogs.com/jilodream/p/5786036.html
Like what:
1. The instance method in the interface can be used directly
public interface userinterface{
public static void Methodstatic () {
}
Public default Methoddefault () {
}
}
public class test{
userinterface inter = new UserInterface ();//You can treat this interface as a generic abstract class as new instance
inter.methodstatic;//a static method that calls it directly
}
2. The default method, which can be passed directly to the class that implements the interface (the test class does not implement the interface call directly, the test class implements the interface, and then the test class implements the interface after the override is called)
public class test{
userinterface.super.methoddefault;//does not implement direct invocation of the interface, note the need to use "super."
}
PS: The other two ways, here is not an example.
, the same point
1, the interface is also a class, she also has her own member variables and member methods;
Three, interfaces and different points of abstract classes
In fact, after the JAVA8 version, her two differences are not obvious (by the above "special attention" can be found).
1. Different definition methods
Class definition: test{}
Definition of interface: interface inter{}
2. Different ways of being used
Class inheritance: XX extends test{}
Implementation of the interface: XX implements inter{}
PS: In use, the biggest difference is that a class can implement multiple interfaces at the same time-->XX implements inter1,inter2{}
3. Different internal properties
Internal properties of the class: No restrictions are made.
Internal properties of the interface: must be public static final (that is, the member variable must be a constant), and the interface is a collection of constants and abstract methods (of course, after JAVA8, the interface can also have an instance method and the default method).
Now know so much, back in the learning process there are new discoveries, and then to add ~ ~ ~ ~
Talking about Java interfaces and abstract classes