Several Basic concepts commonly used in java

Source: Internet
Author: User

The member variables in the Interface1. interface are of the public, static, and final types by default, and must be explicitly initialized. 2. The methods in the interface are of the public and abstract types by default. 3. The interface can only contain public, static, final member variables and public, abstract member methods. 4. The interface cannot be instantiated because it does not have a constructor. The constructor defined in the interface is invalid. 5. One interface cannot implement another interface, but it can inherit multiple other interfaces. Interface C extends A, B {}// both A and B are interfaces 6. when a class implements an interface, it must implement all the abstract methods in the interface; otherwise, the class must be defined as an abstract class. 7. there are no method body access control modifiers in the interface. Four access levels are available: public, protected, default, and privateprivate. You can only access the same class by default, to access protected in the same package, you can access public in the same type and in the same package and subclass (The subclass can not be in the same package) without limiting the available modifiers of the category class: abstract, public, final construction methods: public, protected, private local variables: only final abstract modifier 1. abstract classes can have no abstract methods, but classes that contain abstract methods must be defined as abstract classes. 2. There is no abstract constructor and no abstract static method. Static and abstract cannot be used together. 3. abstract classes can have non-abstract constructor methods. These constructor methods may be called when a subclass instance is created. 4. abstract classes and abstract methods cannot be modified by final modifiers. Final and abstract cannot be used together, and private and abstract cannot be used together. 5. abstract classes cannot be instantiated. Because the abstract class may contain the final modifier of the abstract method without the method body; unchangeable; meaning, it can modify non-abstract classes, non-abstract member methods and variables 1. classes modified with final cannot be inherited, and no subclass exists. 2. Methods Modified with final cannot be overwritten by the quilt class. 3. The variable modified with final indicates a constant. It can only be assigned a value once and must be explicitly initialized. 4. The default private modification method is the final method. Therefore, the method of the quilt class cannot be overwritten. Static modifier 1. static variables only have one copy in the memory and are shared among all instances of the class. 2. You cannot directly access instance methods and instance variables in static methods. 3. The this and super keywords cannot be used in static methods. 4. Static methods cannot be abstract. 5. Both static methods and static variables can be directly accessed by class names. 6. When the class is loaded, the static code block is executed only once. Different static code blocks in the class are sequentially executed by the public static void main (String args []) {} program entry method in the order they appear in the class, which can be modified by final. Super keyword 1. In the class constructor, the super statement is used to call the constructor of the class's parent class. In the constructor, the super statement must be the first statement of the constructor. 2. Access the blocked methods and attributes of the parent class in the subclass. 3. You can only use the super keyword in the constructor or instance method. Overwrite the super keyword with the method related to a specific instance of the class (method rewriting) 1. the parameter signature and return value of the method must be of the same type. The access control modifier can be different, but the subclass method cannot reduce the access permission of the parent method. 2. The exception thrown by the subclass method must be the same as that thrown by the parent method, or the subclass of the exception class thrown by the parent method. 3. The static method of the parent class cannot overwrite the non-static method of the quilt class. Non-static methods of the parent class cannot be overwritten by the quilt class as static methods. 4. Subclass can define a static method with the same name as the static method of the parent class to hide the static method of the parent class in the subclass. Difference: During the runtime, JVM binds the static method to its class, and binds the instance method to its instance. 5. the private method of the parent class cannot be overwritten. 6. the non-abstract method of the parent class can be overwritten as an abstract method overload. 1. two methods with the same name and identical Parameter Signatures are not allowed in a class. 2. The same function of the class may have multiple implementations. The implementation method depends on the parameters specified by the caller. 3. for methods of the class (including methods inherited from the parent class), if two methods have the same method name but different parameters, you can say that one method is an overloaded method of the other method. Polymorphism 1. For a variable of the reference type, the Java compiler processes it according to the declared type. Base base = new Sub (); www.2cto. combase. subMethod (); // compilation error, Base class, Sub subclass. The Compiler considers base as a reference variable of the Base type, and the Base class does not have the subMethod () method. In this way, you must use forced conversion. (Sub) (base). subMethod (); 2. For a variable of the reference type, the runtime JVM processes the variable according to the actually referenced object. Base base Base = new base (); Sub sub = (Sub) Base; // compiled, but ClassCastException is thrown at runtime, the reference type variable of the subclass can be converted to the reference type of the parent class, but the opposite process is not. If yes, that is, the above two lines of code run normally, then sub. subMethod (); During execution, the JVM will not find the subMethod. Sub actually references an instance of the Base class. 3. In the runtime environment, JVM uses the following binding rules to access the methods and attributes of referenced objects by referencing type variables. ① The instance method is bound to the method of the object actually referenced by the reference variable, that is, dynamic binding. ② The static method is bound to the method of the type declared by the reference variable, that is, the static binding. The binding is completed in the compilation phase. ③ The member variables (static variables and instance variables) are bound to the member variables of the type declared by the reference type. Static binding.

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.