Java object-oriented-interface

Source: Internet
Author: User

Java object-oriented- interface

Interface definition: A special kind of "abstract class", there is no ordinary method, which is composed of global constants and public abstract methods;

1, definition of the interface

Interface definition with keyword interface

Let's give the example code:

1  PackageCOM.JAVA1234.CHAP03.SEC12;2  3 /**4 * Define an interface a5  * @authorUser6  *7  */8  Public InterfaceA {9  Ten     /** One * Global Constants A      */ -      Public Static FinalString title= "Hello"; -       the     /** - * Define an abstract method abstraction can be omitted -      */ -      Public Abstract voida (); +}

Note: Because the interface methods are abstract , so abstract can be omitted, the actual development is generally omitted, the habit of developers;

2, implementation interface can implement one or more interfaces

Implement interface We use the Implements keyword , we come up with a test method:

1  PackageCOM.JAVA1234.CHAP03.SEC12;2  3  Public classTestImplementsa{4  5 @Override6      Public voidA () {7System.out.println ("a Method");8     }9  Ten      Public Static voidMain (string[] args) { OneTest t=NewTest (); A t.a (); - System.out.println (test.title); -     } the}

Implement the interface, in the class to implement the abstract method in the interface;

Run output:

A method

Hello

To demonstrate implementing multiple interfaces, we define an interface here:

1  PackageCOM.JAVA1234.CHAP03.SEC12;2  3 /**4 * Define an interface B5  * @authorUser6  *7  */8  Public InterfaceB {9  Ten     /** One * Global Constants A      */ -      Public Static FinalString title2= "Java Knowledge sharing network"; -       the     /** - * Define an abstract method abstraction can be omitted -      */ -      Public Abstract voidb (); +}

We modify the following test class:

1  PackageCOM.JAVA1234.CHAP03.SEC12;2  3  Public classTestImplementsA,  b{4  5 @Override6      Public voidA () {7System.out.println ("a Method");8     }9      Ten @Override One      Public voidB () { ASystem.out.println ("B method")); -     } -   the      Public Static voidMain (string[] args) { -Test t=NewTest (); - t.a (); - t.b (); + System.out.println (test.title); - System.out.println (test.title2); +     } A   at       -}

implement multiple interfaces, separated by commas, and implement all abstract methods :

Run output:

A method

B method

Hello

Java Knowledge sharing Network

3, inheriting classes and implementing interfaces inherit first, then implement interface

Let's first define a class C:

1  Package COM.JAVA1234.CHAP03.SEC12; 2  3  Public class C {4  5      Public void C () {6         System.out.println ("C Method"); 7     }8 }

We then modify the test class tests to inherit the C class, implement a A, A/b interface, here is a note,Java specification, must first inherit the interface after implementation;

Our upper and lower code:

1  PackageCOM.JAVA1234.CHAP03.SEC12;2  3  Public classTestextendsCImplementsa,b{4  5 @Override6      Public voidA () {7System.out.println ("a Method");8     }9      Ten @Override One      Public voidB () { ASystem.out.println ("B method")); -     } -   the      Public Static voidMain (string[] args) { -Test t=NewTest (); - t.a (); - t.b (); + t.c (); - System.out.println (test.title); + System.out.println (test.title2); A     } at   -       -}

Run output:

A method

B method

C method

Hello

Java Knowledge sharing Network

4, interface inheritance interfaces can inherit multiple

We define a D interface to inherit A/b interface :

1  Package COM.JAVA1234.CHAP03.SEC12; 2  3  Public Interface extends a,b{4  5      Public void d (); 6 }

Add a D method here;

We re-engage a test method inherits Class C, implementing the D interface:

1  PackageCOM.JAVA1234.CHAP03.SEC12;2  3  Public classTest2extendsCImplementsd{4  5 @Override6      Public voidA () {7System.out.println ("a Method");8     }9  Ten @Override One      Public voidB () { ASystem.out.println ("B method")); -     } -   the @Override -      Public voidd () { -System.out.println ("D method")); -     } +   -      Public Static voidMain (string[] args) { +Test2 t=NewTest2 (); A t.a (); at t.b (); - t.c (); - t.d (); - System.out.println (test2.title); - System.out.println (test2.title2); -     } in}

Run output:

A method

B method

C method

D method

Hello

Java Knowledge sharing Network

Note:

Interfaces in Java cannot be instantiated.

Java object-oriented-interface

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.