Benefits of Inheritance
1. Improve the reusability of the code.
2. Having a relationship between classes and classes provides a precondition for the third feature polymorphism.
Java supports single inheritance and does not directly support multiple inheritance, but improves the multi-inheritance mechanism in C + +.
Java supports multiple layers of inheritance.
C Inherits B,b inherits a, it will appear the inheritance system.
When you want to use an inheritance system:
1. Review the top-level classes in the system to understand the basic functions of the system.
2. Create the most sub-class object in the system and complete the use of the common energy.
When do you define inheritance?
When there is a relationship between classes and classes, the definition of inheritance, XXX is one of the yyy, XXX extends yyy.
Class person{string Name;int age;} Class Student extends person //inherit {//String name;//int age;void Study () {System.out.println (name+ "... Student study ..... "+age);}} Class Worker extends person{//String name;//int age;void work () {System.out.println (' Worker work ');}} Class Extendsdemo{public static void Main (string[] args) {Student s = new Student (); s.name = "Zhangsan"; s.age = 20;s.study ( );}}
In the child parent class, the characteristics of the member are reflected
1. Member variables
2. member functions
3. Constructors
1. Member variables
When a member of this class has the same name as a local variable, this is used to differentiate;
When a member variable in a child parent class has the same name, the parent class is distinguished by super.
This and super are very similar in usage.
This: Represents a reference to an object of this class.
Super: Represents the space of a parent class and does not represent the parent class object.
Class Fu{int num = 4;} Class Zi extends Fu{int num = 5;void Show () {System.out.println (super.num+ "..." +num);}} Class Extendsdemo2{public static void Main (string[] args) {Zi z = new Zi (); Z.show ();}}
2. member functions
When a member function is identical in a child parent class, the function of the subclass is run.
This behavior, called the overwrite operation, is the attribute of the function in the child parent class.
Function two properties:
1. Overloading. In the same class, overload
2. Overwrite. Subclass, overrides are also called Overrides, override
Coverage considerations:
1. When a subclass method overrides a parent class method, the child class permission must be greater than or equal to the parent class's permission.
2. Static can only be covered by static, or static override.
Class Fu{void Show () {System.out.println ("Fu show Run");}} Class Zi extends Fu{void show () {System.out.println ("Zi show Run");}} Class Extendsdemo2{public static void Main (string[] args) {Zi z = new Zi (); Z.show ();}}
When do I use overrides?
When you extend a subclass to a class, the subclass needs to preserve the function declaration of the parent class.
However, to define the unique content of the feature in the subclass, it is done with the overwrite operation.
Class Phone{void Call () {}void Show () {//System.out.println ("pic");//System.out.println ("name"); System.out.println ("number");}}
Application Scenarios
Now the phone has been upgraded with the ability to display names and pictures.
But if I directly in the source code modification will increase the cost of modification, not good.
Then we can use inheritance to solve.
Class Newphone extends Phone{void show () {SYSTEM.OUT.PRINTLN ("pic"); System.out.println ("name");//System.out.println ("number"); Super.show ();}} Class Phonedemo{public static void Main (string[] args) {//Phone p = new Phone ();//P.show (); Newphone p = new Newphone ();p. Show ();}}
Inheritance of constructors in child parent class
When you construct an object in a subclass, the parent class also runs when the child class constructor is discovered.
Why is it?
The reason is that the first row in the constructor of the subclass has a default implicit statement---super ();
Instantiation of subclasses: All constructors in a subclass access the constructor of the null argument in the parent class by default.
Class Fu{fu () {System.out.println ("Fu Run");}} Class Zi extends Fu{zi () {//super (); The default implicit statement. Called is the constructor of the null argument in the parent class. System.out.println ("Zi Run");}} Class Extendsdemo4{public static void Main (string[] args) {new Zi ();/* Result: Fu Runzi run*/}}
There is another situation
Class Fu{fu () {System.out.println ("Fu Run");} Fu (int x) {System.out.println ("Fu run ...." +x);}} Class Zi extends Fu{zi () {//super (); The default implicit statement. Called is the constructor of the null argument in the parent class. System.out.println ("Zi run ..."); Zi (int x) {System.out.println ("Zi run ...." +x);}} Class Extendsdemo4{public static void Main (string[] args) {new Zi (4);/* Result: Fu Runzi run .... 4*/}}
Why do you want to access the constructor in the parent class when the subclass is instantiated?
That's because the subclass inherits the parent class and gets the content (attributes) from the parent class, so before you use the parent class content, you first see how the parent class initializes its own content.
So when a subclass constructs an object, it must access the constructor in the parent class.
Why do this necessary action, the son of the constructor of the sub-class plus super (). Statement.
If the null argument constructor is not defined in the parent class, then the constructor of the subclass must use super to explicitly call which constructor in the parent class.
At the same time, if this class constructor is called with this in the subclass constructor,
Then super is gone because super and this can only define the first row. So there's only one.
However, it is guaranteed that other constructors in the subclass will have access to the constructor of the parent class.
NOTE: The super statement must be defined in the first row of the subclass constructor, because the initialization action of the parent class is completed first.
Class Fu{fu () {super (); show (); return;} void Show () {System.out.println ("Fu Show");}} Class Zi extends Fu{int num = 8; Zi () {//super (); The default implicit statement. The constructor that invokes the null argument in the parent class .//--> the member variable of the subclass does not appear initialized when the parent class content is initialized with super.//wait for super () after the initialization of the parent class is complete, the member variables of the subclass are initialized.//That is, to get busy with the parent class, Then you can be busy with your own business. System.out.println ("Zi cons run ..." +num); void Show () {System.out.println ("Zi show ..." +num);} Class Extendsdemo5{public static void Main (string[] args) {Zi z = new Zi (); Z.show ();/* Result: Zi show ... 0zi Cons Run ... 8zi show ... 8*/}}
The process of instantiating an object:
Person p = new person ();
1. The JVM will read the Person.class file under the specified path and load it into memory.
The parent class of the person (if there is a direct parent class) is loaded first.
2. Open space in heap memory and assign addresses.
3. And in object space, the properties in the object are initialized by default.
4. Call the corresponding constructor to initialize.
5. In the constructor, the first row calls the constructor in the parent class to initialize.
6. After the parent class is initialized, the properties of the child class are displayed for initialization.
7. Perform a specific initialization of the subclass constructor.
8. When the initialization is complete, assign the address to the reference variable.
Java learning the inheritance of personal memos