Inheritance: Extends creates a new class from a class, the new class---subclasses, the original class---The parent class, the superclass, the base class subclass inherits the parent class, the subclass has the content of the parent class (except the constructor), and the subclass can also have its own unique content. A parent can have more than one child class, and a subclass must have only one parent class---java single inheritance. Pros: 1. Create a more special type 2. Eliminate code duplication problems 3. Facilitates maintainability and improves extension the subclass inherits the parent class, including the private member, but the inherited private member cannot be accessed directly in the subclass, and must pass public access. Method overrides/overrides: In subclasses, the same method as the parent class appears, but the method body content is different, the subclass object is established, the method is called, and the content of the subclass is run. Note The method override differs from the method overload in that the method overload is independent of the parent class, and the method parameter list differs from the same name. Requirements: 1. Must have child parent Class 2. When the subclass overrides the method, the access control modifier must be greater than or equal to the parent Class 3. Method signatures must be consistent jdk5.0 the return value type of the subclass override method can be a subclass of the parent class that returns a value type 4. The overridden method should not be privatesuper: Reference to the parent class object this: a reference to the current class object calls the parent class constructor: There is an implicit super () statement in the first row of the subclass constructor. The purpose of this statement is: 1. Call the parent class with no arguments to the constructor 2. The property used to initialize subclass inheritance 3. If the first row of the subclass constructor is not this (...) or Super (...) NOTE: this (...) or Super (...) Can occur only once in the constructor, and in the first row of the constructor. Class Test{public static void Main (string[] args) {pc p = new PC ("Red"); System.out.println (P.color); System.out.println (P.getdetails ()); System.out.println (P.say ()); System.out.println (P.getdetails ());}} class computer{private string cpu;int memory;int harddisk;public string getdetails () {return memory+ "" +hardDisk;} Public computer (String cpu,int memory,int harddisk) {this.cpu = Cpu;this.memory = Memory;thiS.harddisk = harddisk;} Public computer (String CPU) {This ("i5", 4,250);} string say () {return cpu+ "" +memory+ "" +harddisk;}} class pc extends computer{string color;public pc (String cpu,string color) {super (CPU); this.color = color;} public PC (String color) {This ("i7", color);} @Overridepublic String getdetails () {return color+ "" + Say ();}} Class Notepad extends Computer{int size;public Notepad (String cpu,int size) {super (CPU); size = 14;} public int GetSize () {return size;}} UML: Known as the Unified Modeling language, graphical symbols represent the graphical language of object-oriented elements. Class diagram: Represented by a rectangle. Part I: Class Name box The second part: property box permission modifier property name: Data Type Part III: Method box permission modifier method Name: Return value type permission modifier: public: +protected: #default: ~ or empty private:-
Java Handy Note two inheritance