JavaSE review diary: interface, javase review diary Interface

Source: Internet
Author: User

JavaSE review diary: interface, javase review diary Interface

/** Interface * Reference Data Type: * class, interface, array; ** interface is a reference data type, which can be considered as a special class, it is designed to solve the problem of weak functions caused by no multi-inheritance. A class can only have one parent class, but this class can implement multiple interfaces; ** the interface can also be seen as a class with only abstract methods, that is, all methods of the interface must be implemented. * The keyword declared by the interface is interface, instead of class; the inheritance keyword extends of a common class also becomes implements (Implementation). * However, extends serves the same purpose as ordinary classes, just to differentiate them; * ** 1 defines the interface Syntax: * [modifier list] interface name {} * 2 content in the interface: * can only be a constant (public static final constant name) and abstract METHODS * 3 abstract methods do not require abstract modification. By default, they are public (not required) * 4 interfaces can be considered as a special abstract Class is completely abstract. No common method * 5 interfaces do not have constructor methods, that is, interfaces cannot be instantiated. * 6 A class can implement multiple interfaces, however, you can only inherit one class * 7 Non-abstract class implementation interface. You need to implement all methods of the interface (rewrite/overwrite) * What About the abstract class implementation interface? * Abstract class implementation interface, which can be any: 0 ~ N ** 8 interfaces: * can be inherited. In addition, interfaces can also be inherited. In contrast, a class can only inherit a single ** constant and static data: * Initialization is performed when classes are loaded, but static data can be assigned multiple times. ** constant: public static final must be used to declare constants in interfaces, these can be omitted. * naming method: naming with uppercase letters and underscores. ** Note: The above section must differentiate the "inheritance" between interfaces ", "Implementation" between classes and interfaces, "inheritance" of classes and classes; */public class JavaSE {public static void main (String [] args) {F f = new F (); f. m1 (); f. m2 (); f. m3 (); f. m4 () ;}} interface A {// interface A public void m1 () ;}interface B {// interface B public void m2 ();} interface C {// interface C void m3 (); // the public of the method in the interface can be omitted;} interface D extends A, B, C {// The interface inherits multiple void m4 ();} class MyClass implements B, C {// The implementation of common classes on the Interface public void m2 () {System. out. println ("override B interface's m2 () method");} public void m3 () {System. out. println ("override the m3 () method of the C interface") ;}} abstract class E implements D {// implement public static final int A = 10 for the interface of the abstract class; // constant declaration method char C = '中'; public void m1 () {} public void m2 () {} public void m3 () {} public void m4 () {}} class F extends E {// all methods of E need to be overwritten public void m1 () {System. out. println ("override m1 () method");} public void m2 () {System. out. println ("override m2 () method");} public void m3 () {System. out. println ("override m3 () method");} public void m4 () {System. out. println ("overwriting the m4 () method in D ");}}

 

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.