Inheritance of Java learning notes

Source: Internet
Author: User

Inheritance of Java learning notes
1. Inheritance basis in Java terminology, the inherited class is called superclass, And the inherited class is called subclass ). example: Copy code 1 class Box 2 {3 public double width; 4 public double height; 5 public double depth; 6 7 // reload Construction Method 8 public Box (Box ob) 9 {10 width = ob. width; 11 height = ob. height; 12 depth = ob. depth; 13} 14 15 public Box (double w, double h, double d) 16 {17 width = w; 18 height = h; 19 depth = d; 20} 21 22 public Box () 23 {24 width =-1; 25 height = -1; 26 depth =-1; 27} 28 29 public Box (double len) 30 {31 width = height = depth = len; 32} 33 34 // calculates the volume 35 public double volume () 36 {37 return width * height * depth; 38} 39} 40 41 // The following classes inherit from the Box42 class BoxWeight extends Box43 {44 double weight; 45 46 // The BoxWeight constructor 47 BoxWeight (double w, double h, double d, double m) 48 {49 width = w; 50 height = h; 51 depth = d; 52 weight = m; 53} 54} 55 56 public class DemoBoxWeight57 {58 public static void main (String args []) 59 {60 BoxWeight mybox1 = new BoxWeight (10, 20, 15, 34.3); 61 BoxWeight mybox2 = new BoxWeight (2, 3, 4, 0.076); 62 double vol; 63 64 vol = mybox1.volume (); 65 System. out. println ("Volume of mybox1 is" + vol); 66 System. out. println ("Weight of mybox1 is" + mybox1.weight); 67 System. out. println (); 68 vol = mybox2.volume (); 69 System. out. println ("Vol Ume of mybox2 is "+ vol); 70 System. out. println ("Weight of mybox2 is" + mybox2.weight); 71} 72} copy the code, as shown in row 42. Declare a class that inherits the superclass and use the keyword extends. The format is as follows: class subclass-name extends superclass-name {// body of class} sub-class BoxWeight includes all the members of the super class Box. This is why the sub-class in row 49-51 can directly assign values to the super class members, in addition, the subclass object mybox1 can call the superclass method volume. In addition, a subclass can be a superclass of another class. However, a subclass only supports one superclass (unlike C ++, a derived class in C ++ can inherit multiple base classes). No class can be its own superclass. Running result: One of the main advantages of inheritance is that once you have created a super class, the super class defines the attributes that apply to a group of objects, it can be used to create any number of sub-classes that describe more details. Each subclass can create its own category. The BoxWeight class above inherits Box and adds a weight attribute. Each subclass only adds its own unique attributes. Ii. Access Permissions and Inheritance of Members although the sub-class includes all the members of the super class, it cannot access the members declared as private in the super class, A class member defined as private is private to this class and cannot be accessed by all code outside the class. There are usually four types of access control for Class Members: public, protected, default, and private. This section summarizes the allowed access ranges of various control modes: 3. Super variables can reference subclass objects to copy code 1 class Box 2 {3 public double width; 4 public double height; 5 public double depth; 6 7 // overload constructor 8 public Box (Box ob) 9 {10 width = ob. width; 11 height = ob. height; 12 depth = ob. depth; 13} 14 15 public Box (double w, double h, double d) 16 {17 width = w; 18 height = h; 19 depth = d; 20} 21 22 public Box () 23 {24 width =-1; 25 height =-1; 26 depth =-1; 27} 28 29 public Box (double len) 30 {31 width = height = depth = len; 32} 33 34 // calculates the volume 35 public double volume () 36 {37 return width * height * depth; 38} 39} 40 41 // The following classes inherit from the Box42 class BoxWeight extends Box43 {44 double weight; 45 46 // The BoxWeight constructor 47 BoxWeight (double w, double h, double d, double m) 48 {49 width = w; 50 height = h; 51 depth = d; 52 weight = m; 53} 54} 55 class RefDemo 56 {57 public static void main (String args []) 58 {59 BoxWeight weightbox = new BoxWeight (3, 5, 7, 8.37 ); 60 Box plainbox = new Box (); 61 double vol; 62 63 vol = weightbox. volume (); 64 System. out. println ("Volume of weightbox is" + vol); 65 System. out. println ("Weight of weightbox is" + 66 weightbox. weight); 67 System. out. println (); 68 // assign BoxWeight reference to Box referen Ce 69 plainbox = weightbox; 70 71 vol = plainbox. volume (); // OK, volume () defined in Box 72 System. out. println ("Volume of plainbox is" + vol); 73 74/* The following statement is invalid because plainbox 75 does not define a weight member. */76 // System. out. println ("Weight of plainbox is" + plainbox. weight); 77} 78} the copy code weightbox is a reference of the BoxWeight object, and plainbox is a reference of the Box object (the reference concepts in JAVA are somewhat different from those in C ++. Take an exam at http://blog.sina.com.cn/s/blog_7fb1495b01012sfn.html, which is detailed ). Since BoxWeight is a subclass of Box, it is allowed to assign values to plainbox by referencing a weightbox object, but plainbox cannot access weight, because the superclass does not know the attributes added to the subclass weight, therefore, if the last line is commented out, it is impossible for the Box reference to access the weight domain because it does not define this domain. 4. There are two general forms of super. The first method is to call the constructor of the superclass. The second type is used to access the hidden super-class members of the quilt class. Use super to call the superclass constructor and consider the improved BoxWeight () version below: copy the code class BoxWeight extends Box {double weight; // BoxWeight constructor BoxWeight (double w, double h, double d, double m) {super (w, h, d); // call the superclass constructor weight = m ;}} to copy the code so that the Box can fully copy the width, height, the depth statement is private. When initializing these Members, the subclass does not initialize them by itself. Instead, it calls the constructor of the superclass to initialize these values (the constructor of the superclass itself can access its own private member ), this facilitates the encapsulation of superclasses. In addition, the superclass will decide which constructor to call according to the form of parameters in super. See the following program: copy the Code 1 class Box 2 {3 // all the Members are "private" 4 private double width; 5 private double height; 6 private double depth; 7 8 // reload constructor 9 public Box (Box ob) 10 {11 width = ob. width; 12 height = ob. height; 13 depth = ob. depth; 14} 15 16 public Box (double w, double h, double d) 17 {18 width = w; 19 height = h; 20 depth = d; 21} 22 23 public Box () 24 {25 width =-1; 26 height =-1; 27 depth =-1; 28} 29 30 public Box (double len) 31 {32 width = height = depth = len; 33} 34 35 // calculate the volume 36 public double volume () 37 {38 return width * height * depth; 39} 40} 41 42 // The following classes inherit from the class Box 43 class BoxWeight extends Box 44 {45 double weight; 46 47 // use super to call the construction method of BoxWeight 48 BoxWeight (BoxWeight ob) 49 {50 super (ob); 51 weight = ob. weight; 52} 53 54 BoxWeight (double w, double h, double d, double m) 55 {56 super (w, h, d); 57 weight = m; 58} 59 // default constructor 60 BoxWeight () {61 super (); 62 weight =-1; 63} 64 65 BoxWeight (double len, double m) {66 super (len); 67 weight = m; 68} 69} 70 public class myJavaTest 71 {72 public static void main (String args []) {73 BoxWeight mybox1 = new BoxWeight (10, 20, 15, 34.3); 74 BoxWeight mybox2 = new BoxWeight (2, 3, 4, 0.076 ); 75 BoxWeight mybox3 = new BoxWeight (); // default 76 BoxWeight mycube = new BoxWeight (3, 2); 77 BoxWeight myclone = new BoxWeight (mybox1); 78 double vol; 79 80 vol = mybox1.volume (); 81 System. out. println ("Volume of mybox1 is" + vol); 82 System. out. println ("Weight of mybox1 is" + mybox1.weight); 83 System. out. println (); 84 85 vol = mybox2.volume (); 86 System. out. println ("Volume of mybox2 is" + vol); 87 System. out. println ("Weight of mybox2 is" + mybox2.weight); 88 System. out. println (); 89 90 vol = mybox3.volume (); 91 System. out. println ("Volume of mybox3 is" + vol); 92 System. out. println ("Weight of mybox3 is" + mybox3.weight); 93 System. out. println (); 94 95 vol = myclone. volume (); 96 System. out. println ("Volume of myclone is" + vol); 97 System. out. println ("Weight of myclone is" + myclone. weight); 98 System. out. println (); 99 vol = mycube. volume (); 100 System. out. println ("Volume of mycube is" + vol); 101 System. out. println ("Weight of mycube is" + mycube. weight); 102 System. out. println (); 103} 104}

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.