"Java Summary" final keyword, abstract class, interface, inner class

Source: Internet
Author: User
Tags multiple inheritance in java

Final
    1. The final decorated class, which indicates that the class cannot be inherited
    2. The final modification method, which indicates that this method cannot be overridden
    3. Final modifier constant (constant capitalization), indicating that constants cannot be modified
    4. Static final-modified constants, representing global constants
Abstract class
    • Class that uses the abstract modifier, which is a class of abstraction
    • Using the abstract modified method, as an abstraction, this method only life is not implemented
    • Classes that contain abstract methods must be abstract classes
    • Abstract class cannot be instantiated, subclass of abstract class must override abstract method of parent class
    • Cannot use the abstract modifier property, private method, constructor, static method, final method
/*我为抽象类*/abstract calss A {    abstractvoid m();}
Interface (interface)
    • An interface is a special abstract class with no attributes, only methods
    • The interface is implemented by the class, and the implementing class must override all the methods in the interface
    • Interface is a way to implement multiple inheritance in Java
    • Interfaces are public-decorated, and methods are public
/*声明一个接口*/publicinterface Runner {    publicvoidrun();}/*实现接口*/publicclass implements Runner {    publicvoidrun() {        System.out.printf("我会跑!");    }}
Inner class

In Java, one class is allowed to define another class, which becomes an outer class, which is called an inner class

    • Inner classes can be declared as final
    • Inner classes can be decorated as private or protected
    • The inner class can be static, and the non-static member variable of the outer class cannot be used at this time
 Public classTest { Public Static void Main(string[] args) {A A =NewA (); A.B B = A.NewB (); B.M (3); }}class A {Private ints =1; Public classBPrivate ints =2; Public void m(ints) {System. out. println (s);//3 Local VariablesSystem. out. println ( This. s);//2 objects for inner classesSystem. out. println (A. This. s);//1 objects of the outer class}    }}
Anonymous inner class

General Swing is used more
Here is a use of the inner class

button.addListener(    new OnClickListener() {        //实现方法     });

"Java Summary" final keyword, abstract class, interface, inner class

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.