AbstractAbstract class: Do not know what is the specific type of things. Abstract class name
1. Abstract classes cannot be directly new.
2. Abstract classes can have no abstract methods.
3. In an abstract class, there can be abstract methods or non-abstract methods. You can also have member variables in an abstract class.
4. In a derived class, you can not implement some abstract methods, but derived classes must be declared as abstract classes.
Second, abstract method: Do not know the specific is the way to do something. Abstract return type function name (formal parameter);
1. The abstract method has no function body.
2. A class that has an abstract method must be an abstract class.
Encountered abstract class, two usages:
1. Find out if there is a derived class for it, with its derived class.
2. Write a class yourself, derived from this abstract class.
Package com.itnba.may.Demo1229; Public Abstract class USB { // abstract class // Two abstract methods Public abstract void Qidong (); Public Abstract void Tingzhi ();}
Interface
Can be understood as a special kind of abstract class. There are only constants and abstract methods (no need to write abstract).
Interfaces can be implemented more (inherited), and abstract classes can only be inherited by units.
Defined:
Interface interface Name [extends parent interface List]{
Final type constant name = value;
public void method Name (formal parameter);
}
Public Interface USB { // defines an interface public void qidong (); Public void Tingzhi ();}
Implementation of the interface:
[Abstract] Class name extends parent class implements interface name, interface name,.... {
}
Public class Implements usb{ @Override publicvoid Qidong () { System.out.println ("Mouse Run" ); } @Override publicvoid Tingzhi () { System.out.println ("Mouse unplugged" ); } // if a method with an interface is not rewritten, the class is converted to abstract abstraction. }
When the interface is defined:
1. The interface as small as possible, the function is too complex. --Interface separation principle.
2. When the interface is defined, the interface name, as far as possible adjectives, or describe the nature of the word.
Final modification uses final to modify member variables, member methods, and classes.
1.final Decorated class: This class cannot be inherited.
2.final modified member Variable: can be used as represented.
3.final Retouching method: Cannot overwrite (override) The method of the quilt class, but can be inherited.
Java abstraction, interface, and final