Interface: interface
It can be considered as a special abstract class.
When methods in abstract classes are abstract, the class can be expressed in the form of interfaces.
Class is used to define a class
Interface is used to define interfaces.
Interface Definition, format features:
1. Common definitions in Interfaces: constants and abstract methods
2. All the members in the interface have fixed modifiers.
Constant: public static final
Method: public abstract
Tip: all members in the interface are public.
3. Interfaces cannot create objects because there are abstract methods.
The subcategory can be instantiated only when it needs to be implemented by the quilt class and all the abstract methods in the interface are overwritten by the subclass.
Otherwise, the subclass is an abstract class.
4. interfaces can be implemented by multiple classes and are not supported by multi-inheritance. java supports multiple implementations.
Code Demonstration:
[Java]
Interface Inter
{
Public static final int PI = 3.14;
Public abstract void show ();
}
Class InterA
{
Public abstract void method ();
}
Class Demo
{
Public void function (){}
}
Class Test extends Demo implements Inter, InterA // not only can be implemented but also inherited
{
Public void show (){}
Public void method (){}
}
Class InterfaceDemo
{
Public static void main (String [] args)
{
Test t = new Test ();
System. out. println (t. Num );
System. out. println (Test. Num );
System. out. println (Inter. Num );
}
}