1. Inheritance of classes
1) Inheritance
Parent class: Properties and behaviors that are common to all child classes
Subclasses: Properties and behaviors specific to subclasses
- Implementing class inheritance by extends keywords
- Subclasses (sub Class) can inherit member variables and member methods of the parent class (Super Class), and can also define their own member variables and member methods
- The Java language does not support multiple inheritance, a class can inherit only one parent class (single inheritance), but a parent class may have multiple subclasses
- Inheritance has transitivity, subclasses of subclasses can inherit member variables and member methods of the parent class's parent class
2) Inheritance in constructor method (Super keyword)
- Java rule: You must construct a parent class before you construct a subclass
- The constructor of the parent class must be called through the Super keyword in the constructor of the subclass, so that the member variables inherited from the parent class can be properly initialized
- If the constructor of the parent class is not called in the constructor of the subclass, the Java compiler automatically joins the call to the parent class without a parameter construction method (if the parent class does not have a parameterless constructor, a compilation error occurs)
- Super () The parent class construct must be in the first sentence of the subclass construct
- Super refers to the parent class object of the current object
How to use Super:
- Super. Member variable name: Accessing member variables of the parent class
- Super. Method Name (): Calling methods of the parent class
- Super (): Call the parent class's constructor method
Case 1: Calling the parent class without a parameter construct
public class Person {String Name;char gender;} public class Student extends person {//super (); Compilation error, must be located in the first sentence of the subclass construction method double score; Student (double score) {super (); The compiler will automatically add This.score = Score;super.name = "Tom" by default;}}
Case 2: Calling the parent class has a parameter construct
public class Person {String Name;char gender; Person (String Name,char gender) {this.name = Name;this.gender = Gender;}} public class Student extends person {//super (); Compilation error, must be located in the first sentence of the subclass construction method double score; Student (String Name,char gender,double score) {//super (); Compile error, no parameterless construct super (Name,gender) in parent class; Calling the parent class has a parameter construct This.score = Score;super.name = "Tom";}}
3) Upward styling
- An object of a subclass can be styled as the type of the parent class. That is, a reference that defines a parent type can point to an object of a subclass
- A reference to a parent class can point to an object of a subclass, but a reference to the parent class only accesses the members defined by the parent class and cannot access the part of the subclass extension (what can point out what to look at)
Case 3: Styling up
public class Person {String Name;char gender; Person (String Name,char gender) {this.name = Name;this.gender = Gender;}} public class Student extends person {double score; Student (String Name,char gender,double score) {super (name,gender); Calling the parent class has a parameter construct This.score = Score;super.name = "Tom";} public static void Main (string[] args) {person p = new Student ("Tom", ' Male ', "n"); Upward modelling p.score = +; Compilation errors, the Java compiler checks whether the calling method matches based on the type of reference (person), not the type of the object (Student). }}
2. Override of method (override)
- Occurs in a parent-child class, the method signature is the same, the method body is different: Subclasses can override (overwrite) methods that inherit from the parent class, that is, the method name and the argument list are the same as the methods of the parent class, but the methods are implemented differently (the method body differs)
- When the overriding method is called, look at the type of the object: When the overridden method of a subclass object is invoked (whether by reference to a subclass or by a reference to a parent class), the overridden version of the subclass is run
- Subclasses can invoke the version of the parent class with the Super keyword When overriding the method of the parent class, which is typically used to extend the functionality of the subclass's overridden methods on the basis of the parent class method.
Case 4: Method overrides
public class Student {public static void main (string[] args) {Goo o = new Goo (); O.f (); Foo oo = new Goo (); Oo.f ();}} Class foo{public void F () {System.out.println ("foo.f ()");}} Class Goo extends foo{public void f () {System.out.println ("goo.f ()");}} When an overriding method of a subclass object is called (whether through a reference to a subclass or a reference to a parent class), the overridden method of the subclass is run. /* Run Result: GOO.F () goo.f () */
Case 5: Method overrides, super Call parent class version
public class Student {public static void main (string[] args) {Goo o = new Goo (); O.f (); Foo oo = new Goo (); Oo.f ();}} Class foo{public void F () {System.out.println ("foo.f ()");}} Class Goo extends foo{public void f () {super.f (); Call the parent class's method System.out.println ("Goo.f ()");}} SUPER.F () in the subclass rewrite method; The version of the parent class is called, and the syntax is typically used to extend the functionality of the subclass's overriding method on the basis of the parent method. /* Run Result: FOO.F () goo.f () foo.f () goo.f () */
Java inheritance and overrides, overloading