1, the inheritance relationship between classes and classes is generated by the extends keyword. When the same properties and behaviors exist in multiple classes, the content is extracted into a separate class, so that multiple classes do not need to define these properties and behaviors, as long as the class is inherited, the existing class is called a superclass, a base class, or a parent class. The new class is called a subclass, a derived class, a child class. subclasses can access non-private properties and behaviors directly in the parent class. Subclasses cannot inherit content that is private in the parent class. Java does not support multiple inheritance, only support single inheritance, multi-implementation. inheritance increases the reusability of code, and causes relationships between classes and classes. Provides a precondition for polymorphism. 2, the Super keyword represents the identifier of the member variable memory space in the parent class. Two functions: Call a superclass method or variable, 2, call the superclass constructor. 3, overriding, when a member function that appears in a child parent class is identical, the function of the child class is run. This phenomenon is called the overwrite operation. A method of a parent class can be replicated when a subclass needs the functionality of a parent class, while a feature body subclass has its own unique content. Note the difference between overrides and overloads. The parent class private method and the static method cannot be overwritten, but are hidden. When overridden, the subclass's method permission must be greater than or equal to the parent class permission. Allows subclasses to define the return type of the overriding method as a subtype of the original return type. 3, constructor, subclass constructor by default call subclass default constructor. The subclass is initialized first, and then the parent class. The parent class constructor must be placed in the first row. Object initialization order, as follows: An object instantiation process, with person P = new person (); For example: 1, the JVM reads the Person.class file under the specified path, loads it into memory, and loads the parent class of the person first, if there is a direct parent class. 2, open up space in memory, and assign address. 3, and in object space, the object's properties are initialized by default. 4. Call the corresponding constructor to initialize. 5. In the constructor, the first row is initialized by calling the constructor in the parent class first. 6. After the parent class is initialized, the properties of the child class are displayed and initialized. 7. Then the specific initialization of the subclass constructor. 8. When the initialization is complete, assign the value of the address to the reference variable. 4, Final keyword: final can be decorated with classes, methods, variables. The final decorated class cannot be inherited. The final modified method cannot be overridden. A final modified variable is a constant that can only be assigned once. Why use final modifier variables, in fact, if a data is fixed in the program. Then use this data directly, but the reading is poor, so you should give the data a name. And the value of this variable name cannot be changed, so add final fixed. Specification: Constant All letters are capitalized, multiple words, in the middle with _ connection.5, abstract class, abstract is from a number of things will be common content extracted. Java can define a method without a method, the concrete implementation of which has a subclass complete, called an abstract method, which is called an abstract class. Abstract classes and abstract methods must be decorated with the abstract keyword.
abstract classes are extracted from concrete objects, which are not concrete, have no corresponding instances, and cannot be instantiated. An abstract class is instantiated by its subclasses, and the subclass needs to overwrite all abstract methods in the abstract class before the object can be created, or the subclass is an abstract class. is there a constructor in an abstract class? A: Yes, it is used to initialize the subclass object. Abstract keyword Abstractions can not coexist with which keywords? answer: Private, static, final. Final: You cannot override its method in a subclass definition. Can there be no abstract method in an abstract class? answer: Yes, but it's rare. The goal is to not allow the class to create objects, which is the type of the AWT adapter object. Usually the method in this class has a method body, but there is no content. Same point:Both abstract and generic classes are used to describe things, and members are defined internally. different points:① General classes have enough information to describe things. abstract classes may not have enough information to describe things. ② You cannot define abstract methods in a generic class, you can only define non-abstract methods. Abstract methods can be defined in abstract classes, and non-abstract methods can also be defined. ③ Generic classes can be instantiated. Abstract classes cannot be instantiated. 6, interface,when a method in an abstract class is abstract, the abstract class can be defined and represented in another form, which is the interface. ① Although global variables in abstract classes and modifiers for abstract methods can be used without writing, this is a poor reading. So, it's best to write. The ② class is an inheritance relationship with the class and the interface is the implementation relationship directly. the ③ interface cannot be instantiated, only if the subclass of the interface is implemented and all the abstract methods in the interface are overwritten. Otherwise, this subclass is an abstract class. What
are the similarities and differences between abstract classes and interfaces?
Same point:
are constantly upward drawn.
Different points:
① abstract classes need to be inherited and can only be inherited.
Interfaces need to be implemented and can be implemented more.
Abstract methods and non-abstract methods can be defined in the ② abstract class, and after the subclass inherits, non-abstract methods can be used directly.
Only abstract methods can be defined in an interface and must be implemented by subclasses.
③ the inheritance of abstract class, is a relationship, defines the basic common content of the system.
The implementation of the interface is like a relationship. 7, polymorphic, an object variable can indicate that a variety of actual types of phenomena are called polymorphism. is the "is-a" rule.
Embodiment:
The reference to the parent class or interface points to or receives its own subclass object.
Role:
The existence of polymorphism improves the expansibility of the program and the maintainability of the latter.
Prerequisites:
① need to have inheritance or implementation relationships.
② requires a overwrite operation.
Benefits:increases the extensibility of the code, which can be used later by the code defined in the previous section.
Disadvantages:
Pre-defined content cannot use (invoke) the specific content of the late sub-class. in Java, a subclass array reference can be converted to a superclass array reference, without forcing a type conversion, but it is important to remember that the type of the array is created at all times. instanceof: Used to determine the specific type of object, can only be used to reference data type judgment, usually before the downward transformation for robustness of the judgment, NULL does not error.
member Variables
Compile-time: refers to whether there are member variables called in the class to which the referenced variable belongs. There, compile through, no, compile failed.
Runtime: Refer to whether the referenced variable belongs to a class that has a calling member variable, and run the member variable in the owning class.
Simply put: both compile and run refer to the left side of the equals sign.
member function (non-static)
Compile time: Refers to whether the referenced variable belongs to a class that has a calling function. There, compile through. No, compilation failed.
Runtime: Refers to whether there are functions called in the class to which the object belongs.
Simply put: Compile to look to the left and run to see the right.
static functions
Compile time: Refer to whether the object belongs to a class that has a function called.
Runtime: Refers to whether there are functions called in the class to which the object belongs.
Simply put: Compile and run look to the left.
8, dynamic binding, object method invocation time step, candidate method, overload resolution, call. If the private method, the static method, the final method, or the constructor, then the compiler will be able to know exactly which method to call, which is called static binding. By contrast, the method invoked relies on an implicit parameter type and implements dynamic binding at run time. The compiler uses dynamic binding to generate a directive that calls its methods. When the program is running and the method is invoked with dynamic binding, the virtual machine must call the method that is most appropriate for the actual type of the object referenced by X. Each time the method is called for a search, the overhead is considerable, so the virtual machine creates a method table in advance for each class that lists the signatures of all methods and the methods that are actually called. 9,object class, the superclass of all classes, using = = to compare basic types,Equals compares the object field, comparing whether two objects have the same reference. Equals () Method: Reflexive (x! =null, X.equals (x) =ture), Symmetry (x.equals (y) = y.equals (x)), transitivity (x=y, y= Z, then X=z), consistency (if the reference object does not change, the result will not change), If a subclass can have its own equality concept. The symmetry requirement will be forced to use getclass for detection. If the superclass determines the concept of equality, then it can be instrumented with instanceof, which can be used for equality detection in different subclasses. @override prevents the parent class method from being overwritten.The hashcode () method, which has an integer value exported by the object. The default is the storage address of the object. There is a content component in the string. If you redefine the Equals method, you also need to redefine the Hashcode method. Their definition must be the same. Clone () method, deep copy requires the Clone () method, which is the Protect method that the user cannot call directly. In general: If you want to make a deep copy, you must redefine the Clone method. for each class, you need to make the following judgments:1, whether the default clone method satisfies the requirement. 2, whether the default clone method can be patched by invoking the clone of a mutable child object. 3, if clone should not be used. to select 1 or 2, then:implements the Cloneable interface. (The interface acts only as a token, and an exception is thrown if an object needs to be cloned without implementing the Cloneable interface.) Can now return subclasses of object class, bevel return type)redefine the Clone method with the public access modifier. 10, Object Wrapper, Integer, Long, Double, Float, short, byte,chararter,void, Boolean (the first 6 are derived from the number Class). Packing: Xxx.valueof () Unpacking: Xxx.xxvalue (), automatic packing specification requires boolean,byte,char<= 127. Short and int between -128~127 are wrapped in a fixed object. 11, variable parameter, used as array. Enum Classes: You can compare directly with = =, without equals comparison. If you want, you can add some constructors, methods, and fields to the enumeration class. Of course, the constructor is simply called when constructing an enumeration constant. All enum types are subclasses of the Enum class, and the common method is ToString (). ValueOf (class, String), values (). 12, interface and callback, callback is a common programming pattern in which a particular event can be identified. Because an object can carry some additional information, it is much more flexible to pass an object than to pass a function. 13, Inner class,A class is defined inside another class, which is called an inner class (built-in class, nested Class). Why use internal classes?Inner classes can access members in external classes directly, including private members. inner classes can be hidden from other classes in the same package. When you want to define a callback function that does not want to write a large amount of code, it is easier to use anonymous inner classes. References to external classes: Outer.this outside the scope of the class, you can use the inner class: the Outerclass.innerclass compiler translates the inner class into a regular class file that splits the external class name and the inner class name. The compiler generates THIS$0 variable external object references. All the names will be added outer$. The inner class accesses the outer class private domain, which generates a static method in the outer class. This has security implications. The constructor is also generated.The inner class is defined at the member position and can be decorated by the private, static member modifier. An inner class that is modified by static can only access static members in an external class. If a static member is defined in an inner class, the inner class must also be static! The inner class is defined at a local location, and you can access the members of the external class directly. Local class modifiers cannot be declared with the public private access descriptor, and can be completely hidden from the outside world, even if the external class method is inaccessible. You can also access local variables in the local area, but must be final decorated. The inner class backs up the final constant and the external class this. In order to keep the local variables consistent with the local copy of the inner class. anonymous inner class:
Definition:is the simplified notation of the inner class.
Premise:
An inner class can inherit or implement an external class or interface.
Format:
The new external class name or interface name () {overrides the code in the class or interface, or you can customize the content. )}
Simple to understand:
is to create a subclass of an external class or interface with Content anonymous objects.
When do you use anonymous internal classes?
An anonymous inner class can be passed as a parameter, typically when the usage method is an interface type parameter, and there are no more than three methods in the interface. Because the name of the constructor must be the same as the class name, and the anonymous class does not have a class name, the anonymous class cannot have a constructor and instead passes the constructor to the superclass constructor. The inner class declared in the interface is automatically public and static by default. Tip: Double-brace initialization. arraylist<string> friends = new ArrayList () Friends.add ("Nihao"); Friends.add ("Hell:"); invite (Friends); Can be simplified to: invite (new arraylist<string> () {{Add ("Hary"); Add ("Tony");}}); Constructs a code block.
Dark Horse programmer--java Learning Note Four (inheritance, interfaces, inner classes)