Three main features of object-oriented: encapsulation: mainly implements the hiding of details, and provides users with access interfaces, without the need to care about the specific implementation of methods. Inheritance: it achieves code reuse and improves programming efficiency. Polymorphism: Enhanced Program scalability and maintainability. Inheritance is a mechanism for creating new classes from existing classes. It refers to the extension of functions based on existing classes. Inheritance can be divided into child classes and parent classes. There are two important members: member variables and methods. Some of the sub-class members are declared and defined by the sub-class itself, and the other part is inherited from its parent class. 1. the so-called subclass inherits the member variables of the parent class as one of its member variables, as if they are directly declared in the subclass, any instance method operation declared by itself in the quilt class. 2. the so-called subclass inherits the methods of the parent class as a method in the subclass, just as they are directly declared in the subclass, you can call any instance method declared by yourself in the quilt class. (1) Java only supports single inheritance, that is, only one parent class, which is inherited using the extends keyword; class A {} class B {} class C extends, B {// error, because two parent classes are inherited at the same time} (2) java allows multi-level inheritance of class A {} class B extends A {} class C extends B {} (3) when the subclass has the same method definition as the parent class (that is, the return type, method name, and parameter list are identical, and only the method body is different), it is called method rewriting (OverRide ); 1. For an object created by a subclass, if the subclass overrides the method of the parent class, the system calls the override method of the subclass during runtime. If the subclass inherits the method of the parent class (not overwritten ), the object created by the subclass can also call this method, except that the Behavior Produced by the method is the same as that of the parent class. 2. The access permission of the method cannot be reduced when the parent class method is overwritten. 3. If the method of the parent class is overwritten and the method of the parent class is overwritten, you can use super in the subclass method. override method (); 4. Subclass does not inherit the constructor of the parent class. 5. The constructor of the subclass must call the constructor of the parent class. 6. If the constructor of the subclass does not explicitly call the constructor of the parent class, if the this keyword is not used to call other overloaded constructor methods, the system calls the constructor without parameters in the parent class by default, which is equivalent to skipping super (); statement 7 if no parent class constructor is explicitly called in the subclass constructor, but only the constructor with parameters in the parent class does not have any constructor without parameters, in the subclass constructor, the super (argument_list) statement can be used to call the constructor of the parent class, but must be the first sentence of the constructor. 9 when instantiating a subclass, it actually calls the constructor of the parent class, and then calls the constructor of the subclass. Abstract Class: an Abstract Class provides partial implementation of a type, including instance variables, constructor methods, and Abstract methods. Abstract methods cannot be implemented by methods, even if they are {}) and the specific method. An abstract class does not have instances; it is used for inheritance. It reflects a general/special relationship. The class modified with the keyword abstract is called abstract class (abstract class ). Abstract class {...} Abstract classes have the following features: abstract classes can have abstract METHODS Compared with common classes. For abstract methods, only declarations are allowed, implementation is not allowed, and final modification is not allowed. Abstract class A {abstract int min (int x, int y); int max (int x, int y) {return x> y? The x: y ;}} abstract class cannot use the new operation to create objects. For abstract classes, we cannot use the new operator to create objects of this class. We need to generate a subclass of this class, and create an object by the subclass, if a class is a subclass of abstract class, it must implement the abstract method of the parent class, which is why the final modifier of abstract method is not allowed. An abstract class only cares about whether its subclass has a certain function and does not care about the specific behavior of the function. The specific behavior of the function is implemented by the subclass, abstract METHODS in abstract classes can force subclasses to provide specific implementations of these methods. Polymorphism means that a method of the parent class can generate its own functional behavior when it is overwritten by its subclass. In short, polymorphism is a feature that represents multiple forms. When a class has many subclasses, and these subclasses all override a method in the parent class. Then, when we put the reference of the object created by the subclass into a parent class object, we get a transformation object for this object, therefore, this object may have multiple forms when calling this method. polymorphism 1. for a variable of the reference type, the Java compiler processes the variable according to its declared type. 2. for a variable of the reference type, the Java Virtual Machine processes the variable according to its actual referenced object at runtime. 1. The subclass overrides the parent class. 2. When writing the method, call the method defined by the parent class. 3. At runtime, dynamically decide the method to use based on the actual object type. For example, let the parent class reference point to the subclass object; for example, Person p1 = new Student (); // The referenced type is Person, pointing to the subclass object. The Student interface declares that the interface belongs to the same level as the class. In fact, an interface is a special abstract class. In Java, similar definitions of interface definitions are very similar. They are divided into two parts: interface declaration and interface body. The interface can be declared using the interface keyword. The interface body can only contain global constants and abstract methods. The interface variables are automatically public static final attributes, and the methods in the interface are automatically public abstract attributes, there is no method body, ending with a semicolon. Format: [public] interface name [extends parent interface table] {interface body;} the interface body contains the constant definition and method definition, for example: interface IA {public static final int MAX = 100; // global constant public void print (); // abstract method public abstract int sum (); // abstract method} note: 1) public interface: public interface. Similar to a class, a file can only have one public interface, which is the same as the file name. 2) A public interface and a public class cannot be defined in a file at the same time. 3) an inheritance relationship can be defined between interfaces, and multiple inheritance can be implemented between interfaces. For example, interface IC extends IA, IB {}; interface 1. A class implements the interface using the keyword implements: Format: [modifier] class name [extends parent class] [implements interface] {} non-abstract class, all methods of the interface must be implemented; all abstract methods in the interfaces implemented by the parent class must be implemented in the non-Abstract subclass of the abstract class. 2. in addition to inheriting another class, a class can also implement the interface: class IAImpl extends java. util. arrylist implement IA, IB {} 1) if a class implements an interface, all methods in the interface must be implemented. If no methods in the interface are implemented, this class must be an abstract class. Because a class implements an interface, it inherits an abstract class. 2) Do not write public to methods in the interface, but public cannot be saved in the process of implementing interfaces in sub-classes. (If public is omitted, an error is prompted during compilation: the object cannot implement the method from the interface, which is equivalent to reducing the permission to rewrite the method .) 3) if the parent class implements an interface, the subclass can use this interface without using the implements keyword declaration. 4) the interface can also be used to define the object IA iA = new IAImpl (); // similar to the transformation object features on the transformation object: n the object to be converted cannot operate on the member variables added to the subclass (this part of the attribute is lost); the new method of the subclass cannot be used (some functions are missing ). The transformation object on n can operate on subclass to inherit or hide member variables, or use subclass inheritance or override methods. When the operation subclass of the transformation object inherits or overrides the method on n, it notifies the corresponding subclass object to call these methods. Therefore, if a subclass overrides a method of the parent class, the transformed object of the object must call this method when calling this method. N can forcibly convert the transformation object of the object to a subclass object. In this case, the subclass object has all the attributes and functions of the subclass. Interface use case:
Interface IDetail {public String detail ();} class Teacher implements IDetail {private String name; private int age; private String type; public Teacher (String name, int age, String type) {this. name = name; this. age = age; this. type = type;} // return the instructor details. public String detail () {return "name:" + name + ", age:" + age + ", instructor type: "+ type ;}} class Printer {public void print (String s) {System. out. prin Tln (s) ;}} class MedicalInspection implements IDetail {Printer printer = new Printer (); // return the information of the Health Check Center public String detail () {return "this is the Binzhou health check center, welcome! ";}// Print the detail information public void printDetail (IDetail d) {printer. print (d. detail () ;}} public class ExDemon03 {public static void main (String [] args) {MedicalInspection mi = new MedicalInspection (); IDetail t = new Teacher ("Li Si ", 33, "Java"); mi. printDetail (); // print the instructor information mi. printDetail (mi); // print center information }}