Three features of object-oriented -- Inheritance

Source: Internet
Author: User
Inheritance: The subclass inherits the parent class. The subclass gets all the methods and fields of the parent class, but cannot get the constructor of the parent class. A subclass has only one direct parent class, use the extends keyword.

 

1. subclass will get all methods of the parent class, which does not mean that the subclass object can access all parent class methods. There are three situations:

A. The parent class method is modified using private. The subclass object cannot access this method.

B. The parent class method has the same name as the subclass method (method name and parameter name). The subclass method overwrites the parent class method (or overwrites it) and the subclass object cannot directly access this method. But it can be accessed indirectly. here we need to use the super keyword or parent class name for access. The details will be discussed below.

C. Except for the above two cases, the subclass object can directly access the parent class method.

1 class baseclass 2 {3 private void test () {4 system. out. println ("parent class test method"); 5} 6 Public void base () {7 system. out. println ("parent base method"); 8} 9 void Kass () {10 system. out. println ("Kass method of the parent class"); 11} 12} 13 public class subclass extends baseclass14 {15 public void Kass () {16 system. out. println ("Kass method of subclass"); 17} 18 public static void main (string [] ARGs) {19 subclass BC = new subclass (); 20 // BC. test (); // due to the parent The class test method has the private permission, so it cannot be accessed here. 21 BC. Kass (); // The method is the same. The subclass overwrites the parent class method, and only the subclass method can be accessed here. 22 BC. Base (); // normally access the base method of the parent class. 23} 24}

Here, we also need to describe the sub-classes to override the parent class method:

1. The method name and parameter list are the same. If the child and parent classes use the private modifier, the Child class still cannot access the parent class method.

2. the return value type of the subclass method should be smaller or equal than the parent class, and the exception class thrown by the subclass method declaration should be smaller or equal than the parent class.

3. The access permission of the subclass method should be greater or equal than that of the parent class. Privilege from small to large: the modifier <protected <public is omitted.

4. The sub-and parent-class methods covered by methods are either class methods or instance methods. That is, either static or none of them are used.

The subclass object calls the method that the parent class is overwritten:

1. Class Method: access through the parent class name. Method Name (form parameter list.

2. instance method: use super. Method Name (parameter list.

Note: You can only use the parent class name or super in the subclass method to call the method whose parent class is overwritten.

1 class baseclass 2 {3 static void test () {4 system. out. println ("parent class test method"); 5} 6 Public void Kass () {7 system. out. println ("Kass method of the parent class"); 8} 9} 10 public class subclass extends baseclass11 {12 static void test () {13 system. out. println ("subclass test method"); 14 baseclass. test (); // call the parent class Test Method 15} 16 public void Kass () {17 system. out. println ("Kass method of subclass"); 18 super. kass (); // call the Kass method of the parent class 19} 20 public static void main (string [] ARGs) {21 subclass BC = new subclass (); 22 BC. test (); 23 BC. kass (); 24} 25}
2. subclass will get all fields of the parent class, which does not mean that the subclass object can access all fields of the parent class. There are three situations:

A. The parent class field is modified using private. The subclass object cannot access this field.

B. The parent class field has the same name as the subclass field. The subclass field hides the parent class field, and the subclass object cannot directly access the parent class field. But it can be accessed indirectly. here we need to use the super keyword or parent class name for access. The details will be discussed below. Why is it hidden? Because the Child class still allocates memory space for the parent class feild when creating the object.

C. Except for the above two cases, the subclass object can directly access the parent class field.

1 class baseclass 2 {3 static int A = 5; 4 Public int B = 8; 5 private int c = 11; 6} 7 public class subclass extends baseclass 8 {9 static int A = 5; 10 public int B = 8; 11 private int c = 11; 12 public void test () {13 system. out. println (super. b); // super can only be placed in non-static methods. 14 system. out. println (super. c); // The Field modified by the parent class private cannot be accessed in the subclass, so the error 15} 16 public static void main (string [] ARGs) will be reported) {17 subclass BC = new subclass (); 18 system. out. println (baseclass. a); // the field with static modification uses the parent class name. field name calls 19 BC. test (); 20} 21}

Note: If you access the feild named a in a subclass method, but no super or parent class name is called, the system will look for the order of:

1. Check whether the current method has a local variable named.

2. Check whether the current class is a feild.

3. Check whether the parent class contains a feild named A. trace all the parent classes of A until the java. Lang. Object Class is found, and a compilation error occurs.

3. the constructor of the parent class cannot be obtained, but the initialization code of the parent class constructor can be called.

You can use this call to call another overloaded constructor in one constructor, and call the parent class constructor In the subclass constructor to use the super call, the super call must be performed in the first line of the constructor code.

No matter whether super is used to call the initialization code of the parent class constructor, The subclass constructor always calls the parent class constructor once. If the parent class constructor is a non-argument constructor, no information will be output.

Creating any object always starts from the constructor of the top-level class in the inheritance tree where the class is located, and then runs down to the class constructor.

1 class baseclass 2 {3 Public int A; 4 Public String B; 5 Public baseclass (int A, string B) {6 this. A = A; 7 This. B = B; 8} 9} 10 public class subclass extends baseclass11 {12 public double C; 13 public subclass (int A, string B, double C) {14 super (, b); // the initialization process of the parent class constructor is called through Super 15 this. C = C; // here is this reference 16} 17 public static void main (string [] ARGs) {18 subclass BC = new subclass (12, "hello", 32.21 ); 19 system. out. println (BC. A + "" + BC. B + "" + BC. c); 20} 21}

 

Three features of object-oriented -- Inheritance

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.