Java Abstract, interface and final, java Abstract interface final
Abstract 1. abstract class: a class that does not know what it is. Abstract class name
1. abstract classes cannot be new directly.
2. abstract classes can have no abstract methods.
Public abstract class USB {// abstract class // two abstract methods, the abstract method public abstract void qidong (); public abstract void tingzhi (); // The abstract method does not need to be written {}}
3. abstract classes can have abstract methods or non-abstract methods. Abstract classes can also contain member variables.
4. Some abstract methods may not be implemented in a derived class, but the derived class must be declared as an abstract class.
Public abstract class Shubiao extends USB {public void qidong () {System. out. println ("mouse run");} // The tingzhi () method of the parent class is not overwritten, And the subclass must be changed to abstract .}
2. abstract method: I don't know what the specific method is. Abstract return type function name (shape parameter );
1. the abstract method does not have a function body.
2. classes with abstract methods must be abstract classes.
Abstract classes are used in two ways:
1. Check whether its derived class exists and use its derived class.
2. Write a class by yourself, which is 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
It can be understood as a special abstract class. There are only constants and abstract methods (abstract is not required ).
Interfaces can be implemented (inherited). abstract classes can only be inherited by units.
Definition:
Interface name [extends parent interface list] {
Final type constant name = value;
Public void method name (parameter );
}
Public interface USB {// define an interface public void qidong (); public void tingzhi ();}
Interface implementation:
[Abstract] class name extends parent class implements Interface Name, Interface Name ,....{
}
Public class Shubiao implements USB {@ Override public void qidong () {System. out. println ("mouse run") ;}@ Override public void tingzhi () {System. out. println ("");} // If an interface method is not overwritten, change the class to abstract .}
When defining an interface:
1. The interface should be as small as possible, and the functions are too complex. -- Interface separation principle.
2. When defining an interface, the interface name should be an adjective or a word that describes the nature.
Final can be used to modify member variables, member methods, and classes.
1. final modifier class: This class cannot be inherited.
2. final modifier member variables: can be used as constants.
3. final modification method: the method of the quilt class cannot be overwritten, but can be inherited.