JAVA faces objects (5) -- interfaces and java faces object interfaces

Source: Internet
Author: User

JAVA faces objects (5) -- interfaces and java faces object interfaces

An interface consists of global constants and common abstract methods. The interface definition format is as follows:

1 interface name {2 Global constant; 3 abstract method; 4}

Abstract methods in the interface must be defined as public access permissions. If this parameter is not specified in the interface, the default value is public access.


Interface Definition:

1 interface A {2 public static final String AUTHOR = "Zhang Yu"; // defines the global constant 3 public abstract void print (); // define the abstract Method 4 public abstract String getInfo (); // define the abstract Method 5}

However, the interface definition has declared that the interface is composed of global constants and abstract methods, so it can be simplified to the following format:

1 interface A {2 String AUTHOR = "Zhang Yu"; // equivalent to: public static final String AUTHOR = "Zhang Yu"; 3 void print (); // equivalent: public abstract void print (); 4 String getInfo (); // equivalent to: public abstract String getInfo (); 5}

 

Like an abstract class, to use an interface, you must also use a subclass. The subclass uses the implement keyword to implement the interface:

1 class subclass implement interface A, interface B,... {2}

Implementation interface:

1 interface A {2 public String AUTHOR = "Zhang Yu"; 3 public void print (); 4 public String getInfo (); 5} 6 interface B {7 public void say (); 8} 9 class X implements A, B {// sub-class simultaneously implements two interfaces 10 @ Override11 public void say () {// override the abstract Method 12 System in interface B. out. println ("Hello World"); 13} 14 @ Override15 public void print () {// override the abstract Method 16 System in interface. out. println ("name:" + AUTHOR); 17} 18 @ Override19 public String getInfo () {// override the abstract Method 20 return "Hello" in interface "; 21} 22} 23 public class Demo {24 public static void main (String [] args) {25 X x = new X (); 26 x. say (); // call the method that has been overwritten 27 x. print (); 28} 29}


In addition, interfaces can be inherited and inherited.

1 interface sub-interface extends parent interface A, parent interface B,... {2}

If a subclass inherits the abstract class and the interface, the following format is available:

1 class subclass extends abstract class implement interface A, interface B,... {2}



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.