1. Abstract classes and abstract methods
Abstract method: Incomplete, with only declarations and no method body.
abstract void f();
Abstract class: A class that contains abstract methods. (If a class contains one or more abstract methods, the class must be qualified as abstract.) )
1. It is not safe to create objects directly from an abstract class, because this abstract class is incomplete. In this way, the compiler guarantees
The purity of the abstract class.
public abstract class person {public Abstract void eat (public abstract void pee (public abstract void sleep (//cannot create objects directly with abstract classes //person p = new person ();//error
2. A class inherits from an abstract class, and all the abstract methods in the base class must be implemented if you want to create an object that is a modified class. Otherwise, you will not be able to create objects of that class.
And the export class is also an abstract class.
A class inherits from an abstract class and must implement all the abstract methods in this abstract classPublicClassMaleExtendsperson {@Overridepublic void eat () {System.out.println ( "Male Eat A lot! ");} @Override public void pee () {System.out.println (" Male pee by Standing "); } @Override public void sleep () {System.out.println (" Male sleep "); }}//create an object that changes the exported class person P = new Male ();
3. You can create an abstract class that does not have any abstract methods. (if there is such a class, it doesn't make sense to include any abstract methods, and we want to block any object that produces this class.)
4. Abstract classes do not require all methods to be abstract.
Role:
1. Clarify the abstraction of the class and tell the user and the compiler how to use them.
2. Abstract classes are useful refactoring tools that make it easy to move public methods upward along the inheritance hierarchy (that is, to refactor public method extracts into abstract classes)
2. Interface
The interface keyword makes the abstraction a step further, producing a completely abstract class without providing any concrete implementation. (only method names, argument lists, and return types, there is no method body.) Interface provides only the form, without any specific implementation)
public interface People { void eat(); void sleep(); void pee();}
An interface means: "All classes that implement that particular interface look like this."
The interface is used to establish the protocol between classes and classes (protocol).
Interfaces are more than just an extremely abstract class, because interfaces allow us to create a type that can be transformed up to a variety of base classes
To implement a feature similar to multiple inheritance . (Java only supports single inheritance, but can achieve multi-inheritance effects by implementing interfaces)
Interfaces can contain fields, but these fields are implicitly static and final.
Use the implements keyword to have a class follow an interface (or a set of interfaces). It says: "The interface is just its appearance (what it looks like), we use the class implementation interface to declare how it works"
3. Complete decoupling
-
Policy design Pattern: Creates a method that has different behavior depending on the parameter object being passed (such a method contains the algorithm to be executed
A fixed part, and the policy contains the part of the transformation, the policy is passed in the Parameter object)
-
Adapter Design mode: The code in the adapter will accept the owning interface and produce the required interface.
Decoupling the interface from the specific implementation allows the interface to be applied to a variety of different implementations, so the code is more reusable.
4. The difference between abstract classes and interfaces
Although interfaces and abstract classes look very similar, there are a number of different points between them.
(1). Abstract classes are still classes, so declare them with the class keyword and then use the extends keyword to inherit. The interface is declared with interface, implements inherits.
(2). The method permission in the interface must be public, the variable is a static final type constant, and the method permissions in the abstract class can be set by themselves, as are variables.
(3). All methods in an interface must be abstract, and abstract classes can be abstract or normal.
(4). The last and most important difference is that interfaces support multiple inheritance and abstract classes are not possible! One of the benefits of this is that we can transform a class up into multiple base classes.
Reprint: http://www.cnblogs.com/JohnTsai/p/4659400.html
Java Programming thought Learning (vii) abstract classes and interfaces