Basic concepts of interfaces

Source: Internet
Author: User

If you define a class that consists entirely of abstract methods and global constants, this class is called an interface, and the interface is defined using the interface keyword, and the interface has an abstract method, but the interface object cannot be instantiated.

Define an interface:

Interface A {// define Interface

public static final String INFO = "Hello world.";

public abstract void print ();

}

Interface B {

public abstract void get ();

}

The use principle of the interface:

• Each interface must define subclasses, subclasses use the implements keyword to implement the interface;

• The subclass of the interface (if it is not an abstract class) must overwrite all the abstract methods defined in the interface;

• Instantiate an interface object by using the sub-class of the interface, using an upward transformation of the object.

Subclasses implement the syntax format of the interface:

class sub-class [extends parent class ] [implemetns interface 1, interface 2,...] {}

Each subclass can implement multiple interfaces, but can inherit only one parent class

Example: let subclasses implement interfaces:

Interface A {// define Interface

public static final String INFO = "Hello world.";

public abstract void print ();

}

Interface B {

public abstract void get ();

}

Class X implements A, B {// two interfaces implemented simultaneously

public void print () {//method overwrite

System.out.println ("Hello World");

}

public void get () {

System.out.println (INFO);

}

}

public class Test {

public static void Main (String args[]) {

A = new X ();

b b = new X ();

A.print ();

B.get ();

}

}

If a class implements both an interface and an abstract class, it should be implemented with the first inheritance method

interface A {// Define Interface

public static final String INFO = "Hello world.";

public abstract void print ();

}

Interface B {

public abstract void get ();

}

Abstract class C {

public abstract void Fun ();

}

class X extends C implementsa,b {// two interfaces implemented at the same time

Public void Print () {// method Overwrite

System.out.println ("Hello World");

}

public void get () {

System.out.println (INFO);

}

public void Fun () {

System.out.println (" Hello, world! ") ;

}

}

public class Test {

public static void Main (String args[]) {

A = new X ();

b b = new X ();

c C = new X ();

A.print ();

B.get ();

C.fun ();

}

}

Access to the interface only one, that is: public, even if the definition of the interface is not written on public, it is also public;

In Java, each abstract class can inherit multiple interfaces at the same time, but conversely, an interface cannot inherit an abstract class, but in Java, an interface can inherit multiple interfaces at the same time to implement multi-inheritance operation of the interface.

Interface A {

public void PrintA ();

}

Interface B {

public void Printb ();

}

Interface C extendsA, b {// An interface inherits multiple interfaces

public void Printc ();

}

Class X implements C {

public void PrintA () {}

public void Printb () {}

public void Printc () {}

}

In development, the inner class is never subject to the concept, within a class can define an inner class, within an abstract class can also define the abstract inner class, in an interface can also define internal abstract class or internal interface, but from the actual development, It is relatively uncommon for the user to define an internal abstract class or internal interface itself (the internal interface is seen in Android development), and if static is used when defining an internal interface, it is an external interface.

Interface A {

public void PrintA ();

static interface B {// external interface

public void Printb ();

}

}

Class X implements A.B {

public void Printb () {

System.out.println ("Hello World");

}

}

public class Test {

public static void Main (String args[]) {

A.B temp = new X ();

TEMP.PRINTB ();

}

}

The three main functions of the interface:

• Develop operational standards;

• Representation of a capability;

• Expose the remote method view on the server side to the client.


Basic concepts of interfaces

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.