Java Interface Simple Example

Source: Internet
Author: User

This code altogether consists of 4 classes, under a package:

Example 1:

They are car.java,bigcar.java,testcar.java,carinterface.java, from other people's Web pages to learn, their own experience.

Car.java:

PackageCom.su.callback;

PublicInterfaceCAR {
voidstart ();
voidStop ();
}

Declares an interface, does not implement methods, declares methods only. The suction port cannot be instantiated.

Bigcar.java:

Code PackageCom.su.callback;

PublicclassBigcarImplementsCAR {

@Override
PublicvoidStart () {
//TODO auto-generated Method Stub
System.out.println ("bigcar start ...");
}

@Override
PublicvoidStop () {
//TODO auto-generated Method Stub
System.out.println ("Bigcar stop!");
}

}

Using the Bigcar class to implement the interface car, to implement its method, that is, write program structure. Bigcar can be instantiated, and the Bigcar object is car type. When you need to call the interface
Car method, you need to use car object to call car method, but car can not be instantiated, we can implement the car's Bigcar object to call car method.

Testcar.java

PackageCom.su.callback;

PublicclassTestcar {
PublicvoidOpercar (CAR c)
{
C.start ();
C.stop ();
}
}

Method of calling car using the Opecar method.

Testinterface.java

Code PackageCom.su.callback;

PublicclassTestInterface {
PublicStaticvoidMain (string[] args)
{
Testcar TC=NewTestcar ();
Bigcar BC=NewBigcar ();

Tc.opercar (BC);
}
}

In this example, calling the Opercar method requires an argument to be passed in, and the argument needs to be an object that is an instantiated object of Bigcar (the class that implements the interface).

Output Result:

Bigcar start ...
Bigcar stop!

Summary: When we call the method of interface A: Amethod (), by calling the function of the interface parameter: B (a a) to invoke the function of the interface Amethod (), to invoke B, you need to pass the argument in.

After his own research, wrote himself such as the following:

Example 2:

Interface class: Jk.java

PackageCom.su.callback;

PublicInterfaceJK {
//An interface can have a declaration of several methods
PublicvoidMethodA ();
PublicvoidMethodB ();
}

Interface Implementation Class Implementjk.java

Code PackageCom.su.callback;

PublicclassIMPLEMENTJKImplementsJK {

@Override
PublicvoidMethodA () {
//TODO auto-generated Method Stub

System.out.println ("MethodA implementation of the interface JK. ");
}

@Override
PublicvoidMethodB () {
//TODO auto-generated Method Stub
System.out.println ("METHODB implementation of the interface JK. ");

}

}

The class of the method that invokes the interface Djk.java

PackageCom.su.callback;

PublicclassDJK {
PublicStaticvoidMain (String args[]) {
IMPLEMENTJK IMPLEMENTJK=NewIMPLEMENTJK ();
Implementjk.methoda ();
Implementjk.methodb ();
}
}

Just call Mehtoda, just execute MethodA,

Summary: The interface object can call the interface method, the interface of the implementation class object can also be dropped by the interface method

Example 1, another definition of a class Testcar, using the method of this class (with the interface as parameters) to call the interface of two methods, and then another class TestInterface call Testcar a method to reach the calling interface of the two methods of the role.

Example 2, a method of invoking an interface directly with an instance of an interface's implementation class.

Java Interface Simple Example

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.