Java core technology-5-Inheritance

Source: Internet
Author: User

5 inheritance 5.1 class, superclass, and subclass

Use super to access the parent class. Because the subclass cannot directly access the superclass private domain, the subclass must call the superclass constructor to initialize the superclass private domain. If the constructor of the subclass does not explicitly call the constructor of the superclass, the non-argument constructor of the superclass is automatically called. This purpose: Reference implicit parameters, and call other constructors of this class. Super purpose: first, call the super class method, and second, call the super class constructor. An object variable can indicate that multiple types of objects are called polymorphism. When running, you can automatically choose which method to call is called dynamic binding. 1. The hierarchy is called the hierarchy of all classes derived from a common superclass. 2. Any place where a super-class object appears in a polymorphism program can be replaced by a subclass object. 3. dynamic binding when the called method is private, static, final, or constructor, the compiler can accurately know which method to call. This call method is called Static binding. In contrast, dynamic binding only applies to the public method. Virtual Machine calls Actual typeThe method of the most suitable class. The virtual machine creates Method table, Which lists the signatures of all methods and the actually called methods. When overwriting a method, the subclass method cannot be lower than the superclass method's visibility. 4. block inheritance: The final class and method final class cannot be extended. 5. Reason for forced type conversion: After temporarily ignoring the actual type of the object (use the reference of the superclass to point to and perform some general operations ), use all the functions of the object (forcibly convert to a specific subclass reference and perform the subclass operation ). Before converting a superclass to a subclass, you can use instanceof for type check. 6 In addition to abstract methods, abstract classes can contain specific data and methods. Extended abstract classes: 1. Some abstract methods or abstract methods are not defined in the subclass, so that the subclass must also be marked as an abstract class. 2. Define all abstract methods so that sub-classes are not abstract. Abstract classes cannot be instantiated. 7. protected Access: if some methods of the superclass allow access by the quilt class, or the subclass method is allowed to access a domain in the superclass, the value is set to protected. 5.2 Object: Super class of all classes In java, only the basic type is not an object. 1 The equals method getClass method returns a string that equals Class 2 of an object and inherits the value of the 3 hashCode Method 4 toString method object: class name + field value enclosed by square brackets. ToString method defined by the Object class: Class Name and hash code of the output Object. 5.3 Generic Array list ArrayList is a generic class that uses type parameters. 1. Access the array list element get and set Methods: access the list element. ToArray method: list. toArray (a). Each element of the list is copied to array. 2 compatibility between types and original array lists 5.4 object wrapper and automatic packing Each basic type has a corresponding class. The object wrapper is unchangeable. Once the wrapper is constructed, its value cannot be changed. Automatic packing: the system will list. add (3) is automatically converted to list. add (Integer. valueOf (3) Automatic unpacking: When an Integer object is assigned to an int value, it is automatically disassembled. 5.5 Variable Parameter Quantity Method Public static double max (double... values) {double largest = Double. MIN_VALUE; for (double v: values) if (v> largest) largest = v; return largest;} double defines the type, and the received parameter is an array values []. 5.6 enumeration class Public enum Size {SMALL, MEDIUM, LARGE, EXTRA_LARGE}; when comparing values of two enumeration types, you do not need to call equals and use = directly.

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.