Abstract method
Only a statement, without specifying the content of the method
Abstract methods can not be called directly, needs to be rewritten
statement
The same general method declaration, add the abstract keyword
abstract scopes return type of the method name ();
Abstract class
Contains abstract methods of the class is an abstract class
An abstract class can not be used directly, it can not be instantiated.
effect
Tell subclass for what can be done, but no specific operational content.
Abstract class must be inherited, rewrite abstract methods in a subclass.
(Abstract-oriented programming)
statement
abstract class class name {
…
}
inherit
An abstract class can inherit from abstract class
interface
A special class that contains a public global constants and abstract methods.
effect
Interface multiple inheritance (single inheritance class only)
Determining a class of functions, for programming interface
statement
interface interface name {
Global Constants
Abstract method
}
Because all interface methods are public abstract methods public abstract keyword can be omitted
inherit
- Interface inheritable interfaces, abstract classes can not be inherited and general category
- Inheritance interfaces need to use implements keyword, and can inherit multiple interfaces
-
Ordinary class inherits an interface, the interface must override all methods
eg
// declare an interface A , containing two abstract methods: Print , Area
interface A{
void print();
float area(double r);
}
// declare an interface B , contains abstract methods len
interface B{
float len(double w, double h}
}
// create a common class and inherit from interfaces A and B
class Test implements A,B{
// all methods of the interface must override
@Override
void print(){
…
}
@Override
float area(double r){
return 3.14*r*r;
}
float len(double w, double h){
return w*h*2;
}
}