This article describes the inheritance of Java object-oriented programming. Share it for your reference, as follows:
Inheritance: A special class has all the properties and behaviors of a general class.
Inheritance benefits:
1. Improved code reusability
2. Let the class have a relationship with the class before, and only with this relationship can there be polymorphic characteristics. Inheritance is the relationship between a class and a class before it.
Notes:
1.java only supports single inheritance, not multiple inheritance. Because multiple inheritance has security risks: when multiple parent classes define the same function, but the functions are different, the subclass does not know which one to run.
2. When a child class inherits the parent class, it inherits all methods and properties of the parent class and can be used directly.
3. Java supports multi-layer inheritance, that is, the relationship between grandson-son-father
grammar:
[Class Modifier] class subclass name extends parent class name { statement;}For example:
class Pserson{ int age; String name; public void speak() { System.out.println("Hello World!"); }}//Inherit the Person class, inherit all methods and attributes of the parent class Student extends Pserson{ public void study() { System.out.println("Good Study!"); }}//Inherit the Person class, inherit all methods and attributes of the parent class Worker extends Pserson{ public void work() { System.out.println("Good work!"); }}How to use functions in an inheritance system (see API documentation):
Check the functions of the parent class and create subclass objects to use functions
These three scenarios are often encountered during the inheritance process:
1) Variable with the same name
1. If a subclass has a non-private member variable of the same name, the subclass accesses the variable of this class, and uses this; the subclass accesses the variable of the same name in the parent class, and uses super.
2. This represents a reference to the object of this class
3.super represents a reference to the parent class object (usage is the same as this)
2) Functions with the same name
1. If a subclass appears a function exactly the same as the parent class (the function name and parameters are the same), when the subclass object calls the function, the subclass function content will be run. , the functions of the parent class will be overwritten (also called rewriting).
2. Rewrite the definition: When the subclass inherits the parent class, follows the functions of the parent class, and enters the subclass. However, although the subclass has this function, the content of the function is inconsistent with the parent class. At this time, there is no need to define a new function, but use the override feature, retain the function definition of the parent class, and rewrite the function content.
3. Notes on rewriting (overwrite):
<1>Subclasses override parent class. You must ensure that the permissions of the child class are greater than or equal to the permissions of the parent class before inheritance, otherwise the compilation will fail. (public>Don't write rhetorical keywords>private)
<2> Static can only cover static
<3> Overload: Only look at the parameter list of the function with the same name and rewrite: the child parent class method must be exactly the same (function name and parameter list)
class Fu{ //public void show() When the parent class is show(), it will be exactly the same as the subclass function. The show function of the parent class will be rewritten public void show(String name) //The show function of the parent class is different from the subclass (the parameter list is different), so the show function of the parent class will not be rewritten { System.out.println(name); }}class Zi extends Fu{ public void show() { System.out.println("zi"); }}class Jicheng{ public static void main(String[] args) { Zi z1=new Zi(); z1.show("nihao");//The show function of the parent class will be called}}3) Constructor
1. When initializing the subclass object, the constructor of the parent class will also run, because the first line of the constructor of the subclass has an implicit statement super() by default.
2. super() will access the constructor of the hollow parameters in the parent class, and the first line of all constructors in the subclass is super() by default
3. The reason why subclasses must access the parent class constructor
<1> Because the data subclass in the parent class can be obtained directly, the subclass is to first see how the parent class initializes the data. Therefore, when the subclass is initialized, it first accesses the constructor of the parent class by default.
<2>If you want to access the constructor formulated by the parent class or the constructor whose parent class does not have empty parameters, you can formulate it by manually defining the super statement.
<3> Of course, the first line of the constructor of the subclass can also be manually specified to access the constructor of this class, but at least one of the constructors in the subclass will access the constructor of the parent class.
class Fu{ String name; int age; Fu(){System.out.println("Hello Fu");} Fu(String name) { System.out.println(name); } Fu(String name,int age) { this.name=name; this.age=age; System.out.println("name: "+name+",age: "+age); }}class Zi extends Fu{ //Zi(){System.out.println("Hello Zi");} By default, the parameterless constructor Zi() of the parent class will be called first. super("zhangsan",20);//Manually use the super statement to specify the constructor of the parent class to obtain non-private information of the parent class System.out.println(name+"::"+age); }}class Test{ public static void main(String[] args) { Zi z1=new Zi(); }}Example of constructor exception:
Write out the program results
class Super{ int i=0; public Super(String s) { i=1; }}class Demo extends Super{ public Demo(String s) { i=2; } public static void main(String[] args) { Demo d=new Demo("yes"); System.out.println(di); }}//Compilation failed because the constructor with empty parameters was missing in the parent class. //or the subclass should specify the constructor in the parent class to be called through the super statement.Rewrite and overload examples:
class Demo{ int show(int a,int b){return 0;}}The following functions can exist in a subclass of Demo.
A.public int show(int a,int b){return 0;}//Yes, overwrite.
B.private int show(int a,int b){return 0;}//No, the permissions are not enough.
C.private int show(int a, long b){return 0;}//Yes, it is not the same function as the parent class. No coverage is equivalent to overloading.
D.public short show(int a,int b){return 0;}//No, because this function cannot appear in the same class as the given function, or in the child parent class.
E.static int show(int a,int b){return 0;}//No, static can only overwrite static. <br><br>So the subclass allows for rewriting and overloading.
For more Java-related content, readers who are interested in this site can view the topics: "Introduction and Advanced Tutorial on Java Object-Oriented Programming", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.