Object-oriented (advanced)-basic concepts of interfaces

Source: Internet
Author: User

Object-oriented (advanced)-basic concepts of interfaces

An interface is the most important concept. An interface can be understood as a special class, which is composed of global constants and public abstract methods.
Interface Definition Format:

Interface interface name {
Global constant;
Abstract method;
}

Interface Definition:

Interface {
Public static final string author = "James ";
Public abstract void print (); //
Public abstract string getinfo (); // defines the abstract Method
}

Implementation interface:
1. Similar to abstract classes, interfaces must use subclasses. Subclass uses the implements keyword to implement the interface.
2. Implementation format:
Class subclass implements interface A, interface B ,...{
}

Implementation interface instance:

Interface {
Public static final string author = "James ";
Public abstract void print ();
Public abstract string getinfo ();
}
Abstract class B {
Public abstract void say ();
}
Class X extends B implements {
Public void say (){
System. Out. println ("Hello world !!! ");
}
Public String getinfo (){
Return "hello ";
}
Public void print (){
System. Out. println ("Author:" + author );
}
}
Public class interfacedemo01 {
Public static void main (string ARGs []) {
X = new x ();
X. Say ();
}
}

Interface inheritance

1. An interface cannot inherit an abstract class, but it can inherit multiple interfaces through the extends keyword.
2. Interface: interface sub-interface extends parent interface A, parent interface B ,...{}

Interface multi-inheritance instance

Interface {
Public static final string author = "James ";
Public abstract void printa ();
}
Interface B {
Public abstract void printb ();
}
Interface C extends a, B {
Public abstract void printc ();
}
Class X implements c {
Public void printa (){
System. Out. println ("A. Hello world !!! ");
}
Public void printb (){
System. Out. println ("B. Hello world !!! ");
}
Public void printc (){
System. Out. println ("C. Hello world !!! ");
}
}
Public class interfacedemo02 {
Public static void main (string ARGs []) {
X = new x ();
X. printa ();
X. printb ();
X. printc ();
}
}

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.