1. Inheritance:
Create a new class by extending an existing class and inheriting the class's properties and behavior. Already known as the parent class, the new class is called a subclass (the parent class derives the subclass, and the subclass inherits the parent class).
(1) Advantages of inheritance: The reusability of ① code ;
② the properties of the parent class can be used for subclasses;
③ subclasses can extend the properties and methods of the parent class;
④ the design of the application becomes simpler.
(2) The keyword ' is a ' is used to determine if the inheritance relationship between the parent class and the subclass is correct.
(3) Inherited implementations in Java:
public class subclass extends parent class {}
(4) Inheritance of the single root: In Java, a class can have only one parent class;
Transitivity: A Java class can be a parent class, and a parent class can also have a parent class.
(5) The root class of all classes: The object class, also called the base class or superclass. The object class is the parent class for each Java class, either a direct parent class or an indirect parent class.
Three important methods in the object class:
①public Boolean equals (Object X): detects whether two objects are equal. This method is often overridden. If two objects are judged to be equal by this method, then the two object's The Hashcode () method should produce the same hash code
②public string ToString: The method returns the string representation of the object. Using strings to represent objects is useful for debugging or testing.
③protected void Finalize () throws Throwale: This method is called when an object is about to be garbage collected.
(6) method rewrite: Subclasses can override a method inherited from a parent class that allows subclasses to add or change the behavior of a method in a parent class. the behavior method is born in a subclass and is overridden on a parent class method.
When a subclass overrides a parent class method, the following rules must be followed:
The return value type , method name , and formal argument list of a method of the ① subclass must be the same as in the parent class;
The ② access modifier must be not less than the access modifier in the parent class;
overriding exceptions thrown in ③ subclasses cannot be more than the exceptions thrown in the parent class.
(7) Super keyword: subclasses want to add behavior to inherited methods, but do not completely replace the parent class's methods. (By default, the subclass constructor calls the parent class's parameterless constructor method)
(8) Final keyword: When you do not want a class to be inherited, add it before class keyword final. The final keyword can appear anywhere before the return value type, and is customarily placed after the access modifier.
①final class: A class can be declared as the final class. The final class cannot be inherited.
②fin Al method: A method can be declared as final. The final method cannot be overridden.
Add: The constructor method of the parent class cannot be inherited, the constructor can be called with super (,), and the parent class method is called with super.
(9) Abstract keyword: Use the keyword abstract to declare an abstract class that can appear anywhere in front of the class keyword. You can change a class to an abstract class if it is not instantiated as necessary .
(10) Abstract method: ① has no method body; ② must appear in an abstract class; ③ must be overridden in a subclass, unless the subclass is also an abstract class.
2. Package:
(1) Encapsulation is the technique of making member variables in a class private, and providing the public method to access these member variables, which is also known as data hiding. Function: Ensure the security of the data.
(2) Accessor method: The Get method that allows to get member variables;
Modifier method: A method that allows changing member variables.
(3) The Benefits of encapsulation: the member variables of the ① class can be read-only or write-only;
The ② class can have an overall control over the content stored in its member variables;
Users of the ③ class do not need to know how a class stores data.
eg
1 Public classstudents{2 PrivateString name;3 Public voidsetName (String name) {4 This. Name =name;5 }6 PublicString GetName () {7 returnname;8 } 9}
3. Polymorphism (polymorphism):
(1) refers to the ability of an object to have multiple forms, a subclass of a class can define their unique behavior, while sharing some of the same characteristics of the parent class.
Static polymorphism: ① occurs at program compile time;
② Implementation mode: Method overload;
③ rule: The overloaded method in the corresponding class is called according to the type of the object at the time of definition;
Dynamic polymorphism: ① occurs at run time;
② implementation: Overriding a method of a member of the same name as a parent class
③ Call rule: A method that calls a member of the same name in the corresponding class according to the type of the object at the time of instantiation.
(2) The technical basis of polymorphism: ① upward Transformation Technology: A reference variable of a parent class can point to a sub-class object;
②instanceof keyword: Used to determine the true type of the runtime object (the Java-language polymorphic mechanism causes the declaring type of the reference variable to be inconsistent with the type of the actual reference object, thus referencing the instanceof operator);
③ Dynamic Binding technology: The runtime executes the appropriate subclass method based on the actual type of the object to which the parent refers to the variable.
Upward transformation: From low-precision type to high-precision type to implement automatic conversion;
Downward transformation: A forced type conversion is required to convert from a high-precision type to a low-precision type.
4. Interface-Special abstract Class (interface):
(1) All the ① interfaces are abstract methods and cannot be instantiated;
The ② class cannot inherit the interface and can only "implement" the method defined by the interface;
(2) Interfaces and classes
Similarities:
① interface can have any number of methods;
The ② interface is saved with ". Java "is a suffix file, the interface name needs to match the file name;
The ③ interface is compiled with a byte-code suffix named ". Class".
④ the interface in the package, its corresponding bytecode file must be placed in a directory structure that matches the package name.
The difference:
① interface cannot be instantiated;
The ② interface does not contain any constructors;
All the methods in the ③ interface are abstract;
The ④ interface cannot contain instance member variables, and the only member variable that can appear in the interface must be declared both static and final, which is the public static constant;
The ⑤ interface cannot be inherited by a class and can only be implemented by a class;
⑥ An interface can inherit multiple interfaces.
(3) The interface has the following properties:
the ① interface is abstract by default;
Each method in the ② interface is also abstract by default;
The default access level for methods in the ③ interface is public.
(4) interface use principle:
① using interface to solve multiple inheritance;
② uses interfaces to add functionality to external classes;
③ in an object-oriented perspective, a class of innate characteristics and behavior and dependent on external optional features and behavior separation.
(5) Class implementation interface has 2 choices:
① implements all the methods defined in the interface;
② declares itself as an abstract class.
Use the keyword implements class to implement the interface, which is placed after the extends part of the class declaration
eg
public class class name extends parent class name implements interface name
A class can implement more than one interface, separated by commas.
Implements is an inheritance relationship with is a.
(5) Benefits of using the interface:
① will design and realize the separation of the external hidden implementation;
② interface-oriented programming is the core of OOP.
About inheritance, encapsulation, polymorphism, abstraction, and interfaces