Java Knowledge Point modularity (1)-Object oriented

Source: Internet
Author: User
Tags control characters instance method

Preface this is my Java notes, the module will be in succession, the specific link is as follows: 1. Object-oriented: Http://www.cnblogs.com/Gabby/p/6857406.html 2. Set: 3.io:4. Multithreading: 5.jvm:6. Other knowledge Knowledge Points: Serialization, generics, annotations, exceptions, Reflections: 1. Object-oriented 1.1 object-oriented three major features 1.1.1 Inheritance 1) class inheritance (1) The basic definition of Java inheritance has the characteristics of single inheritance, through the implementation of the keyword extends. Parent class and subclass relationships: generic and special relationships, subclasses are a special form of the parent class, such as fruit and apples. (2) overriding when a method of a parent class cannot satisfy a method of a subclass, you need to override the method of the parent class, overriding it with override. Rewrite steps: Two with two small one big two: the same method name + parameter list is the same two small: subclass return value type <= parent class return value type + Subclass method declaration throws exception <= parent class declaration throws exception a large: Subclass method Access >= Parent class method access permission (3) Super keyword has two Function: Invokes the method of the parent class overridden by the quilt class: Use Super when the overriding method is an instance method, and the parent class name when overriding the method as a class method. The subclass invokes the constructor of the parent class: Super is used to qualify the constructor to initialize the instance variables that the object inherits from the parent class. If it is a class variable, the parent class name is used. (4) The disadvantage of inheritance: it destroys the encapsulation of the parent class. Subclasses can directly access the member variables and methods of the parent class, resulting in severe coupling between the subclass and the parent class. (5) Design the rules to be met by the parent class
    • Hide the internal data of the parent class as much as possible and set the member variable to private;
    • Do not let subclasses arbitrarily access, modify the method of the parent class. The parent class is used as an auxiliary tool method to apply the private adornment; the parent class method requires an external class access, then the public adornment is used, and the parent class method requires a subclass rewrite, then the Protect decoration is used;
    • Try not to invoke a method in the parent class constructor that overrides the quilt class.
(6) Conditions to be met for derived subclasses
    • Subclass is a special kind of parent class, which needs to satisfy the relation of "subclass is parent class";
    • Subclasses need to add additional attributes, not just property value changes
    • Subclasses need to add their own unique way of behaving (including new methods or overriding methods of parent classes)
2) A combination of methods for implementing class multiplexing: inheritance and composition. (1) The combination definition treats the class as a constituent of another class, allowing the new class to reuse the public method of the class directly. Combining the old class object as a member variable of the new class to implement the functionality of the new class, it is often necessary to use the private adornment of the old class method that is combined in the new class. (2) Differences in inheritance and composition
    • Inheritance is a modification of an existing class to obtain a special version. is to change a more abstract class to a class that satisfies special needs, and the inheritance expresses the relation of "is a".
    • The combination expresses the relationship between the whole and the part, and expresses the relation of "has a".
1.1.2 Package 1. The definition hides the object state information inside the object, does not allow the external program to directly access the object internal information, but through the method provided by the class to achieve the operation and access to internal information. 2. Good encapsulation needs to be satisfied: Hide the object's member variables and implementation details, do not allow external direct access, expose the method, and let the method control the safe access and operation of these member variables. 3. Access control characters
  private default protect public
in the same class
in the same package  
subclass    
Global      
1.1.3 Polymorphism 1. Defining a compile-time type is determined by the type used when declaring the variable, and the run-time type is determined by the object that is actually assigned to the variable. If the compile-time type and run-time type are inconsistent, so-called polymorphism can occur. Polymorphic: Variables of the same type, calling the same method to render different behavioral characteristics. An instance variable of an object is not polymorphic. 2. Compile-time polymorphism: Method overloading 3. Runtime Polymorphism: Method overrides 1.2 Class 1.2.1 class composition 1) the member variable (1) Defines the variable defined in the class. is divided into instance variables (not static-modified) and class variables (static-decorated). (2) The class variable class variable uses the static decoration, which starts from the preparation stage of the class, until the system completely destroys the class, the scope of the class variable is the same as that of the class, and (3) The instance variable instance variable is created from the instance of the class, knowing that the system completely destroys the instance, The scope of the instance variable is the same as the living scope of the corresponding instance. (4) Memory running mechanism of member variables when the system first uses a class, it loads the class and initializes the class, and in the preparation phase of the class, the system allocates memory space for the class variable and specifies the default initial value. When class initialization is complete, the class is allocated a chunk of memory that contains the memory of the class variable, and when the system creates an object, the instance variable allocates the memory space and specifies the initial value when the instance is created. (5) Initialization of local variables and operating mechanisms in memory local variables are variables defined in the method. After a local variable is defined, it must be explicitly initialized before it can be used, the system does not initialize the local variable, and the local variable is always stored in the stack memory of the method in which it resides. If the local variable is a variable of the basic type, the value of the variable is stored directly in the corresponding memory of the variable, and if the local variable is a variable of the reference type, the variable is stored in the address, which refers to the object or array that the variable actually refers to in the heap. The variables in the stack memory do not need to be garbage collected by the system, often ending with the execution of the method or block of code. 2) method (1) The method belongs to a class, and the method that has no static adornment belongs to the object. (2) parameter passing mechanism in Java: There is only one way to pass: value is passed. A copy of the actual parameter value is passed in to the method, and the parameter itself is not affected by any effect. The value is passed when the base type is passed, and the reference address value of the object is passed when it is an object. (3) Method overloading: Two identical: The same class in the + method name is the same: The parameter list is different. 3) The constructor (1) defines a special method that performs initialization when the instance is created. Initialization blocks are additions to the constructor (2) constructor overloads use the This keyword to call the constructor of this class 4) initialization block (1) defines the initialization of the Java object. Definition method: [modifier] {//initialization block executableThe code} modifier can only be static. (2) statically initialized block: an initialization block that uses the static modifier. Used to initialize the entire class, typically initializing a class variable. (3) Initialization phase of a class: class initialization: Executing static initialization blocks of the topmost parent class ... Executes the static initialization block object initialization of a subclass: Executes the initialization block of the topmost parent class to perform the constructor of the topmost parent class ... Executes the subclass of the initialization block for the constructor of the subclass 1.3 immutable Class 1. After you define an instance of the class, the instance variable for that instance is immutable. Wrapper classes, java.lang.String are immutable classes. 2. Create a custom immutable class rule: (1) Use the private and final adornments member variables (2) of the class to provide a parameter constructor for initializing a member variable in a class based on the passed-in parameter (3) provides a getter method only for member variables of the class (4) if necessary, overriding Equals and Hashcode methods in the object class 3.String the benefits of immutable classes (1) The string pool is possible only if the string is immutable, and the implementation of the string pool can save a lot of heap space at run time, because different string variables point to the same string in the pool. (2) because the string is immutable, it is thread-safe. (3) The ClassLoader uses a string, and the immutable class provides security to be loaded by the correct class (4) because the string is immutable, hashcode is cached when it is created and does not need to be recalculated. 1.4 Abstract classes and Interfaces 1.4.1 abstract class (1) defines a parent class that knows only what methods its subclasses should contain, but does not know exactly how subclasses implement these methods. Only method signatures, no method implementations. Abstract classes and abstract methods use the abstract adornment. There can be no abstract method in an abstract class. (2) Usage rules
    • Abstract classes and abstract methods must be modified with an abstract method, which cannot have a method body.
    • Abstract class cannot be instantiated, cannot use new to invoke constructor of abstract class to create instance
    • Abstract classes can contain member variables, methods (both normal and abstract), constructors, initialization blocks, internal classes (interfaces, enumerations), 5 members, primarily for their subclass invocation
    • Classes that contain abstract methods can only be defined as abstract classes
The 1.4.2 Interface (1) defines the special form of an abstract class, which cannot contain ordinary methods, and all methods in an interface are abstract methods. Using the interface modifier (2) feature
    • Modifiers: You can use public or omit, omit the package permission access control by default.
    • An interface can inherit multiple parent interfaces, but interfaces can inherit only interfaces and cannot inherit classes.
    • The interface cannot contain constructors and initialization block definitions; Interfaces can contain member variables (only static variables), methods (only abstract instance methods, class methods, or Default methods), inner classes (containing internal interfaces, enumerations);
    • Static constants are automatically added to the static final modifier;
    • Methods in an interface can only be modified with public and cannot be implemented. Class methods and default methods must have an implementation.
(3) Function: Implemented by class, implements.1.4.3 the difference between abstract class and interface (1) Design purpose
    • Interface is a coupling standard between multiple modules, similar to the "master" of the system;
    • Abstract class, as a common parent of many sub-classes in a system, embodies a template-style design. Can be used as intermediate products, this intermediate product has achieved some of the functions of the system, but not as the final product.
(2) Differences in usage
    • Method: An interface can contain only abstract methods, static methods, and default methods, and cannot provide implementations for ordinary methods; Abstract classes can completely contain abstract methods.
    • Variables: Only static variables can be defined in an interface, ordinary member variables cannot be defined, and static variables can be defined in an abstract class, as well as ordinary member variables.
    • Constructor: The interface cannot contain constructors, and abstract classes can contain constructors.
    • Initialization block: An interface cannot contain an initialization block, and an abstract class can contain an initialization block.
    • A class can only be inherited, and an interface can inherit more than one.
1.5 inner Class (1) defines a class that is defined within a class. (2) function
    • The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class;
    • The inner class can directly access private data for the outer class of the class, the external class cannot access the implementation details of the inner class, and if the external class needs to access members of a non-static inner class, you must display the Create non-static inner class object to invoke access to the instance member.
    • Anonymous inner classes are suitable for creating classes that need to be used only once.
(3) The modifier of the intrinsic class of the attribute can be used private,protected,static. The inner class is a class, there are 4 scopes: the same class (private), the same package (not used), the parent-child class (protected), any location (public). Non-static inner classes cannot have static members. 1.5.1 static inner Class (1) defines an inner class that is decorated with static, and the inner class belongs to the external class itself. (2) Features
    • A static inner class can contain either a static member or a non-static member;
    • Static inner classes can only access static members of external classes;
    • The static inner class is parasitic in the class itself of the outer class, holding only the class references of the outer class.
    • A static inner class is a static member of an external class, so all methods of an outer class, initialization blocks can use static inner classes to define variables, create objects, and so on.
    • The outer class can be accessed using the class masterpieces of the static inner class for the caller.
1.5.2 local inner class definitions in methods local inner classes cannot be decorated with the access control and the static modifier 1.5.3 Anonymous inner class (1) defines a class that is suitable for creating only one use at a time. (2) Rules
    • Anonymous inner classes must inherit and inherit at most one parent class or implement an interface;
    • An anonymous inner class cannot be an abstract class, and a constructor cannot be defined.
(3) Creating a method requires creating an object of an interface type: New Implementation Interface | The parent class constructor {class body part}1.6 keyword 1.6.1 static (1) is used to differentiate member variables, methods, inner classes, initialization blocks of which four members belong to the class itself or to the instance. (2) A member of a feature static modifier cannot access a member without a static modifier; a static decorated member indicates that it belongs to the class itself, called a class variable or a class method; 1.6.2 this and super (1) This function: points to the object that called the method. Let a method in the class access the instance variable or method of the class. Usage: Used in the constructor to represent the object that the constructor is initializing, and the object in the method that represents the call to the method is referenced. (2) Super use: Call the instance method overridden by the parent class in the subclass; Super is used by the constructor to indicate that the constructor initializes the instance variable that the object inherits from the parent class. (3) The difference between this and super super calls the constructor of its parent class; This invokes the overloaded constructor in the same class. 1.6.3 Final (1) defines a method that cannot be changed and does not want subclasses to inherit from the parent class. (2) functions are used to modify classes, variables, and methods to represent classes, variables and methods immutable, and once the initial value is assigned, it cannot be changed. (3) Usage modifier member Variable: Class variable: You must specify the initial value at the static initialization block or at the time of declaration; instance variable: must be used in a non-static initialization block or declared variable or constructor; Modify local variable: Specify the initial value when declaring. (3) Features
    • The final modified member variable needs to be assigned at the declaration or constructor or initialization block, and the initial value must be explicitly specified by the programmer.
    • The final decorated base type requires that the value remains the same, cannot be re-assigned, and the final decorated reference type requires only the address to remain unchanged, and the contents of the object referenced by the reference type variable can be changed.
    • The final method does not want subclasses to override a method of the parent class, but can be overloaded; methods using public cannot be overridden, and private can be used to define an identical method.
    • The final decorated class can have no subclasses.
1.7 Other 1.7.1 Packaging Class (1) The source defines the corresponding reference type for 8 basic data types. The class name is the first letter of the base type capitalized. An instance of a wrapper class is a reference type. Why boxing: Because object-oriented thinking, everything is an object (2) automatic boxing and automatic unpacking: A basic type can be directly assigned to the corresponding wrapper class variable or assigned to the object variable automatically unpacking: Allows the wrapper class object to be assigned to the given basic type variable directly. (3) Attribute integer: The system automatically boxes integers between -128~127 into an integer instance and caches them in an array named cache. 1.7.2 string,stringbuffer,stringbuilderstring: The character sequence is immutable; it produces many stringbuffer: Character sequences are variable, thread-safe but performance is not high; use synchronized. StringBuilder: Character sequences are variable, threads are unsafe but performance is slightly better 1.7.3 = = and equals==: Basic type and numeric type, the same value returns True, and the reference type object when it points to the same object returns True;equals: The same string returns true in the constant pool.

Java Knowledge Point modularity (1)-Object oriented

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.