Java notes (7): interface note

Source: Internet
Author: User

I have admired the interface for a long time. Today, I only have the opportunity to fully understand it and create a person. It is always my own mistake.
Java does not support multi-inheritance, but it implements class inheritance through another mechanism. This is the interface. All attributes of an interface are static constants and methods are abstract methods. That is, no method body definition is provided. An interface is equivalent to a set of protocols for program development. Any class that needs to implement such a specific function can inherit the set of these attributes and methods.
Such an interface is equivalent to a class that is not fully implemented, but its attributes and methods are different from each other when they are inherited by a class, A class inherits an interface, which is actually an abstract method in the inherited interface (the method body can be compiled to meet a specific function of the class). Therefore, it inherits an interface through implements, collectively referred to as the implementation interface.

1. Define the interface
An interface is a type of Java Composite data. It is a structure of an image class. It can be seen as an unimplemented class. The interface is defined using the keyword interface, and the file name is saved as the interface name. java.
[Public] interface name [list of extends parent interface names]
{
// Static constant
[Public] [static] [Final] data type variable name = constant value;
// Abstract Method
[Public] [Abstract] [Native] Return Value Type method name (parameter list );
}
The variables defined in the interface are public, static, and final constants, and the methods are abstract and public.

2. Implementation Interface
[Modifier] class name [extends parent class name] [implements interface A, interface B,...]
{
Class member variables and member methods;
Compile method bodies for all methods in interface a to implement interface;
Compile method bodies for all methods in interface B to implement interface B;
}
After a class implements an interface, it will inherit all the static constants in the interface for this class.
After a class implements an interface, if the class is not an abstract class, the class body must write the method body for all the abstract methods in the interface, because the abstract methods cannot exist in non-abstract classes. In this way, the interface can be inherited and implemented.
If the class implementing the interface is an abstract class, the abstract methods in the interface can not be implemented.

3. define interfaces and implementation interface examples
Define the interface areainterface, which has a static constant PAI and an abstract method of area ()

// Define the interface: areainterface. Java
Public interface areainterface
...{
Double Pai = math. Pi;
Double area = ();
}

// The Circle class implements the areainterface interface: circle. Java
Public class circle implements areainterface
...{
Double R;
Public circle (Double X)
...{
R = X;
}
Public double area () // The abstract method in the implementation interface to calculate the circular area
...{
Return Pai * r * R;
}
Public String tostring ()
...{
Retunrn "circle: r =" + R + "Area =" + area ();
}

}
// Class rectangle implements the areainterface interface: rectangle. Java
Public class rectangle implements areainterface
...{
Double X, Y;
Public rectangle (Double X, double B)
...{
X = A; y = B;
}
Public double area () // The abstract method in the implementation interface to calculate the circular area
...{
Return x * Y;
}
Public String tostring ()
...{
Retunrn "rectangle: x =" + x + "; y =" + Y + "Area =" + area ();
}

}
// Two classes after the interface is tested: testinterface. Java
Public class testinterface
...{
Public class void main (string [] ARGs)
...{
Rectangle R1 = new rectangle ();
Circle C1 = new circle ();

System. Out. println ("R1" + r1.tostring ());
System. Out. println ("C1" + c1.tostring ());
}


}

The class library in the Java language also defines a large number of interfaces. A keyevent class is specially written for the keyboard's key-time, which is included in the Java. AWT. Event package. In Java, an interface keylistener is defined. In the same package, the interface has three Abstract METHODS:
Public abstract void keypressed (keyevent); // executed when the key is pressed
Public abstract void keyreleased (keyevent); // executed when the key is released
Public abstract void keytyped (keyevent); // press and release the key and execute

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.