catalogue
Characteristics of inheritance in Java
Considerations for inheritance in Java
Relationship of member variables in inheritance
Relationship between construction methods in inheritance
Relationship between member methods in inheritance
Code block
In Java, the code enclosed by {} is called code block. According to its location and declaration, it can be divided into local code block, construction code block, static code block and synchronous code block (to multithreading).
a. Local code block:
Appear in the method; Limit variable life cycle, release as soon as possible, and improve memory utilization.
In the same method of the same class, if there are multiple local code blocks, the execution order is top-down
public class Test1 { public static void main(String[] args) { //First code block { int a=100; System.out.println(a); } //Second code block { int b =200; System.out.println(b); } //Third code block { int c=300; System.out.println(c); } } }
b. Construction code block:
Occurs outside the method in the class; The same code in multiple construction methods is stored together, and the construction is executed every time it is called, and it is executed before the construction method.
class Demo{ //Create a parameterless constructor Demo(){ int a=10; System.out.println(a); } //Create a construction code block (the code defined outside the methods in the class and enclosed in braces is called a construction code block) { int b=20; System.out.println(b); } } public class Test2 { public static void main(String[] args) { //Create objects for the Demo Demo demo = new Demo(); //Create a local code block { int c=30; System.out.println(c); } } }
It can be seen that when there are both construction methods and construction code blocks in a class, when creating an object, the construction code block will be executed first, and then the construction method will be executed, while the main method is executed from top to bottom.
c. Static code block
Appear outside the method in the class and add static modification; It is used to initialize the class. It is executed when loading, and the value is executed once.
class Demo{ //Create a parameterless constructor Demo(){ int a=10; System.out.println(a); } //Create a construction code block (the code defined outside the methods in the class and enclosed in braces is called a construction code block) { int b=20; System.out.println(b); } //Create a static code block static { int d=40; System.out.println(d); } } public class Test2 { public static void main(String[] args) { //Create objects for the Demo Demo demo = new Demo(); //Create a local code block { int c=30; System.out.println(c); } } }
Static code blocks take precedence over other code blocks and will be executed without creating objects.
Finally, let's integrate:
class Demo3{ Demo3(){ System.out.println("Demo3 Nonparametric construction method of"); } static { System.out.println("Demo3 Static code blocks in"); } { System.out.println("Demo3 Construction code block in"); } } public class Test3 { static { System.out.println("Test3 Static code blocks in"); } public static void main(String[] args) { { System.out.println("main Method"); } Demo3 demo3 = new Demo3(); //Create object for Demo3 } }
Demo3 class needs to create an object to access, so go to Test3 class first, give priority to static code blocks in Test3, and then execute the in the main method. Until the object of demo3 is created, it starts to execute in the order of static code block - construction code block - construction method.
inherit
Inheritance overview
a. When the same attributes and behaviors exist in multiple classes, these contents are extracted into a single class, so multiple classes do not need to define these attributes and behaviors, just inherit that class.
b. Class to class inheritance can be realized through the extends keyword
Format: class subclass name extends parent class name {}
c. A separate class is called a parent class, a base class, or a superclass; These classes can be called subclasses or derived classes. After inheritance, when we define a class, we can define our own new members on the basis of an existing class.
Succession list
class Father{ //Create a parent class and define member variables String name; int age; public void sleep(){ //Define a method System.out.println("sleep"); } } class Son{ //Create a subclass and define member variables String name; int age; public void sleep(){ System.out.println("sleep"); } }
When we create two or more classes that have something in common, we find it very troublesome. For example, both parent and child classes have the same member variables and have a common method, so we can use inheritance to improve.
After improvement:
class Person{ //Define a human and extract the similarities between father and son String name; int age; public void sleep(){ System.out.println("sleep"); } } class Father extends Person{ //Create parent class inherit human } class Son extends Father{ //Create a subclass and inherit the parent class public void play(){ //Create a subclass specific method System.out.println("play a game"); } } public class Extends1 { public static void main(String[] args) { //Create parent object Father father = new Father(); father.sleep(); //The parent class can access the methods of the Person class through inheritance //Create subclass objects Son son = new Son(); son.sleep(); //Subclasses can access the methods of the Person class through inheritance son.play(); //Subclasses can also access their own unique methods } }
After using inheritance, both parent and child classes can access the methods of Person class, and child classes can also access their own unique methods.
Benefits of inheritance
a. It improves the reusability of the code
The same members of multiple classes can be placed in the same class
b. Improved code maintainability
If the function code needs to be modified, just modify one place
c. Making the relationship between classes is the premise of polymorphism
In fact, this is also a disadvantage of inheritance: the coupling of classes is very strong
Characteristics of inheritance in Java
a.Java only supports single inheritance, not multiple inheritance.
A class can only have one parent class and cannot have more than one parent class.
Class subdemo extensions demo {} (correct)
class SubDemo extends Demo1,Demo2... (error)
b.Java supports multi-layer inheritance (inheritance system)
class A{}
class B extends A{}
class C extends B{}
Considerations for inheritance in Java
a. A subclass can only inherit all non private members (member methods and member variables) of the parent class
In fact, this also reflects another disadvantage of inheritance: breaking the encapsulation
b. Subclasses cannot inherit the constructor of the parent class, but the constructor of the parent class can be accessed through the super keyword.
c. To initialize a subclass, you must first initialize the parent class
d. Don't inherit for some functions
Relationship of member variables in inheritance
Case:
class Father2 { //Create parent class int a = 40; int b =50; public void num2() { System.out.println(a); } } class Son2 extends Father2 { //The subclass inherits the parent class int a = 20; public void num1() { System.out.println(a); } public void num3(){ System.out.println(b); } } public class Extends2 { public static void main(String[] args) { //Create subclass objects Son2 son2 = new Son2(); son2.num1(); //According to the principle of proximity, the output should be a in Son2 son2.num2(); //There is no such method in the subclass. Look for it in the parent class son2.num3(); //There is no element b in the subclass. Go to the parent class to find it } }
Conclusion:
Accessing a variable in a subclass method
a. First, find the local scope of the subclass
b. Then find a member in the subclass member range
b. Finally, find in the parent class member scope (the local scope of the parent class must not be accessed)
d. If you still don't, report an error. (regardless of father's father...)
super keyword
class Father3{ int num=30; } class Son3 extends Father3{ int num =40; public void show(){ int num=50; System.out.println(num); //You can access the num in the show method System.out.println(this.num); //Use the this keyword in the encapsulation to access the num in the class } } public class Extends3 { public static void main(String[] args) { Son3 son3 = new Son3(); son3.show(); } }
As mentioned above, we can access the num in the subclass method, or use the this keyword to access the num outside the subclass method. If we want to access the num in the parent class, Java provides a keyword: super
(add System.out.println(super.num) to the above code subclass show method; You can access the num in the parent class)
The usage of super is different from this
This represents the reference corresponding to this class.
super represents the identification of the storage space of the parent class (which can be understood as a parent class reference)
Usage (this and super can be used as follows)
a. Access member variables:
this. Member variable
super. Member variable
b. Access construction method
this(...)
super(...)
c. Access member method
this. Member method ()
super. Member method ()
Relationship between construction methods in inheritance
By default, all constructors in the subclass will access the constructors of the null parameters of the parent class.
Because the subclass inherits the data in the parent class, it may also use the data of the parent class. Therefore, before subclass initialization, you must complete the initialization of parent class data. The first statement of each constructor is: super() by default
Example:
class Father4{ Father4(){ System.out.println("This is a parameterless constructor in the parent class"); } Father4(String s){ System.out.println("This is a parameterized constructor in the parent class"+s); } } class Son4 extends Father4{ Son4(){ System.out.println("This is a parameterless construction method in subclasses"); } Son4(String s){ System.out.println("This is the parametric construction method of subclasses"+s); } } public class Extends4 { public static void main(String[] args) { //Create subclass objects Son4 son4 = new Son4("User"); } }
When there is no parameterless constructor in the parent class:
a. The subclass uses super to display and call other construction methods with parameters of the parent class
Example:
class Father4{ Father4(String s){ System.out.println("This is a parameterized constructor in the parent class"+s); } } class Son4 extends Father4{ Son4(){ super("FU"); System.out.println("This is a parameterless construction method in subclasses"); } Son4(String s){ super("FU"); System.out.println("This is the parametric construction method of subclasses"+s); } } public class Extends4 { public static void main(String[] args) { //Create subclass objects Son4 son4 = new Son4("User"); } }
b. Subclasses use this to call other constructor methods of this class
Other constructs of this class must first access the parent class construct
Example:
class Father4{ Father4(String s){ System.out.println("This is a parameterized constructor in the parent class"+s); } } class Son4 extends Father4{ Son4(){ super("FU"); System.out.println("This is a parameterless construction method in subclasses"); } Son4(String s){ this(); //Using this keyword, first call the parameterless construction method of the subclass and indirectly call the construction method of the parent class System.out.println("This is the parametric construction method of subclasses"+s); } } public class Extends4 { public static void main(String[] args) { //Create subclass objects Son4 son4 = new Son4("User"); } }
c. Be sure to note: super(...) or this(...) It must appear on the first statement, otherwise, there will be multiple initialization of parent class data
Relationship between member methods in inheritance
Member methods with the same name and different names in the child parent class
class Father5{ public void show(){ System.out.println("In the parent class show method"); } public void show3(){ System.out.println("In the parent class show3 method"); } } class Son5 extends Father5{ public void show(){ System.out.println("In subclasses show method"); } public void show2(){ System.out.println("In subclasses show2 method"); } } public class Extends5 { public static void main(String[] args) { Son5 son5 = new Son5(); son5.show(); //Call the show method of the subclass, and follow the proximity principle son5.show2(); //Call the show2 method of the subclass son5.show3(); //The subclass accesses the show3 method. The subclass is not found in the parent class } }
Conclusion: to access a method through a subclass object, first find it in the subclass, and then find it in the parent class. If it still doesn't exist, an error will be reported.
Overview of method rewriting
Subclass as like as two peas in the parent class, is also called method override and method duplication.
Usage features:
If the method name is different, the corresponding method is called
If the method names are the same, the subclass's own method is used
Application of method override:
When a subclass needs the function of the parent class and the subclass of the function subject has its own unique content, you can override the methods in the parent class. In this way, you not only follow the function of the parent class, but also define the unique content of the subclass.
Example:
class Father6{ public void study(String s){ System.out.println("study"+s); } } class Son6 extends Father6{ public void study(String s){ //Override the study method in the parent class System.out.println("study"+s+"Inheritance in"); } public void study2(String s){ System.out.println("study"+s); //Subclass specific methods } } public class Extends6 { public static void main(String[] args) { //Create subclass objects Son6 son6 = new Son6(); son6.study("java"); //Call the study method of the subclass son6.study2("big data"); } }
Method override considerations
a. Private methods in a parent class cannot be overridden
b. When a subclass overrides a parent class method, the access permission cannot be lower
c. The parent class is a static method, and the child class cannot be overridden
The difference between method rewriting and method overloading
Method override s occur in subclasses of inheritance relationships, and method overload s occur in the same class
Overloaded methods have the same name but different parameter lists
Rewriting means that the method name, parameter list and return value are the same, but the implementation is different. It is called method rewriting.
final keyword
Final keyword is the final meaning. It can modify classes, member variables and member methods.
characteristic:
a. Modifier method, method cannot be overridden
Example: when we don't want the subclass to override the methods or functions in the parent class, but only the subclass can use it, we can use final
class Father7{ public final void show(){ //Using the final modifier System.out.println("In the parent class show method"); } } class Son7 extends Father7{ @Override public void show(){ //Subclasses cannot be overridden, otherwise an error will be reported System.out.println("This is in a subclass show method"); } } public class Extends7 { public static void main(String[] args) { Son7 son7 = new Son7(); son7.show(); } }
You can see that if the subclass overrides the method modified by the parent class with final, an error will be reported when running.
b. Class cannot be inherited
c. Modify the variable, and the variable becomes a constant and can only be assigned once (it can be assigned before the construction method is completed)
final modifier local variable
a. within the method, the variable cannot be changed
b. in the method declaration, the basic type and reference type are used as parameters
The basic type is a value that cannot be changed
The reference type is the address value that cannot be changed (the value in the heap memory of the object can be changed)
C. initialization timing of final modified variable
Before the object is constructed