Static, final, abstract, interface, construction method, and Java syntax summary

Source: Internet
Author: User
Tags multiple inheritance in java

Static: When defining a class, you generally do not use static to modify, in a sense, static modified fields can be used as global variables, static modified fields and methods stored in the memory area of the class, all instances shared. Static fields and methods belong to a class, and static methods can only handle statically members of a class and cannot handle non-static members. , the instance variable cannot be accessed in a static method and cannot be used with this and super.

Final: A decorated class that indicates that the class is final and cannot be inherited.

Decorated members, which indicate that this method is final and cannot be overridden by (subclasses), overrides and overloads are two forms of implementing polymorphism. A decorated field indicates that the value of this field is final and cannot be changed after the assignment (there are two ways to assign the final field, the declaration is initialized and the constructor is assigned a value), and when a field is static final decorated, it can represent a constant. The same is true for modifying local variables.

Interface: Java has three reference types, namely classes, interfaces, and arrays. Interfaces appear to compensate for the lack of multiple inheritance in Java, interfaces similar to abstract classes (also different from abstract classes, interfaces can contain fields), define a series of abstract methods, which default to public abstract, the interface is defined by the fact that the class to use, The implements class implements the method of the interface through the keyword, and then it can use the interface.

The declaration of an interface is similar to a class: interface person{}; (No access control is declared, only classes in the package can implement this interface, and interfaces can inherit one or more interfaces via the extends keyword)

Interfaceflyable{intheight=100;//The field in the interface is public static final type, which must be initialized when declared, otherwise the automatic assignment is 0 because it is a static type. The field cannot be changed in the class that implements this interface voidFly ();//default is public abstract} Public classTest4Implementsflyable{/*test4 () {height=100;}*///The method of implementing an interface in a class is to keep the access control of the method consistent with the interface, as well as the signature of the function. is still to write public Public voidFly () {System.out.println ("Fly height is:" +height);}  Public Static voidMain (string[] args) {test4 T1=Newtest4 ();    T1.fly (); }}

Static, final, abstract, interface, construction method, and Java syntax summary

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.