Basic concepts of interfaces -------- interface

Source: Internet
Author: User
Tags define abstract

1. Interface: an interface is a special class, which is composed of a global constant (static final) and a public abstract method (abstract. [Java] interface A {// define interface A public static final String AUTHOR = "Li Xinghua"; // global constant public abstract void print (); // abstract method public abstract String getInfo (); // abstract method} can be simplified during development: [java] view plaincopyinterface A {// define interface A String AUTHOR = "Li Xinghua"; // global constant void print (); // abstract method String getInfo (); // abstract method} the first two methods are the same. 2. Implementation interface: [java] interface A {// defines interface A public String AUTHOR = "Li Xinghua"; // global constant public void print (); // abstract method public String getInfo (); // abstract method} interface B {// define interface B public void say (); // define abstract method} class X implements, class B {// Class X implements both the public void say () {System. out. println ("Hello World !!! ");} Public String getInfo () {return" HELLO ";} public void print () {System. out. println ("AUTHOR:" + AUTHOR) ;}}; public class InterfaceDemo03 {public static void main (String args []) {X x X = new X (); // instantiate the subclass object x. say (); x. print () ;}; 3. inherit the abstract class implementation interface: [java] interface A {// define interface A public String AUTHOR = "Li Xinghua "; // global constant public void print (); // abstract method public String getInfo (); // abstract method} abstract class B {// Define abstract class B public abstract void say (); // define abstract method} class X extends B implements A {// class X inherits class B, then implement the public void say () {System. out. println ("Hello World !!! ");} Public String getInfo () {return" HELLO ";} public void print () {System. out. println ("AUTHOR:" + AUTHOR) ;}}; public class InterfaceDemo04 {public static void main (String args []) {X x X = new X (); // instantiate the subclass object x. say (); x. print () ;}; [java] view plaincopyinterface A {// define interface A public String AUTHOR = "Li Xinghua"; // global constant public void print (); // abstract method public String getInfo (); // abstract method} abstract class B implements A {// define abstract class B and implement interface A public abstract void say (); // define abstract method} class X extends B {// class X inherits class B public void say () {System. out. println ("Hello World !!! ");} Public String getInfo () {return" HELLO ";} public void print () {System. out. println ("AUTHOR:" + AUTHOR) ;}}; public class InterfaceDemo05 {public static void main (String args []) {X x X = new X (); // instantiate the subclass object x. say (); x. print () ;}; 4. interface inheritance: [java] interface A {// define interface A public String AUTHOR = "Li Xinghua "; // global constant public void printA (); // abstract method} interface B {public void printB ();} interface C ext Ends A, B {public void printC ();} class X implements C {// class X inherits class B public void printA () {System. out. println ("A, Hello World !!! ");} Public void printB () {System. out. println ("B, Hello MLDN");} public void printC () {System. out. println ("C, Hello LXH") ;}}; public class InterfaceDemo06 {public static void main (String args []) {X x X = new X (); // instantiate the subclass object x. printA (); x. printB (); x. printC () ;}; 5. Summary

Related Article

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.