Inheritance is one of the three main features of OOP. This section focuses on the inheritance notes.
1. Concept and use of Inheritance
In Java, inheritance can simplify the definition of classes and extend the functions of classes. JAVA supports single inheritance and multi-level inheritance of classes, but does not support multiple inheritance. That is, a class can only inherit one class but not multiple classes.
The implementation inheritance format is: Class subclass name extends parent class
In Java, inheritance can only directly inherit the public attributes and public methods in the parent class.And implicitly (not necessarily) inherits the private attributes. The inherited content is as follows:
2. instantiation of subclass objects
When instantiating a subclass object, note the following: By default, the non-argument constructor in the parent class is called before the subclass constructor is called. For exampleCode:
This is interesting. If the parent class does not have a constructor without parameters, an error will be reported for subclass instantiation ~~
Class person {private string name; private int age; // No-argument constructor of the parent class public person () {system. out. println ("========= constructor in the parent class ========");} // getter and setter Methods Public String getname () {return this. name;} public void setname (string name) {This. name = Name;} public int getage () {return this. age;} public void setage (INT age) {This. age = age;} // return information method Public String getinfo () {return "name:" + name + ", age:" + age ;}} class student extends person {private string school = ""; // public student () {setname ("newsainton"); setage (23 ); system. out. println ("========= constructor In the subclass ========");} public void print () {system. out. println (getinfo () + ", the school is:" + School) ;}} public class demo01 {public static void main (string ARGs []) {// use the subclass object student s = new student (); S. print ();}}
3. Super () method and class method rewrite Mechanism
3.1 super () method
In the above example, the student'sConstructorContains a super () method. this method indicates calling the constructor of the parent class (superclass. you can also use super (parameter type parameter name, parameter type parameter name ,...) this format calls the constructor with parameters after the parent class is overloaded. this super method is used in the following example.
3.2 rewrite of sub-classes to the parent class Method
Subclass can inherit the methods of the parent class, or rewrite the methods of the parent class. however, there is a basic premise for the method of the parent class to be rewritten: The method to be rewritten cannot have more strict access permissions than the parent class method. (access permission public> default> private)
Here is an exampleProgramIn this example, the subclass student calls the constructor of the parent class in the form of super (name, age), and The subclass rewrites the getinfo () of the parent class with unchanged permissions () method:
Class person {private string name; private int age; // You can initialize public person (string name, int age) {This. setname (name); this. setage (AGE);} // The Public void setname (string name) {This. name = Name;} public void setage (INT age) {This. age = age;} Public String getname () {return this. name;} public int getage () {return this. age;} public String getinfo () {return "name =" + this. name + ", age =" + this. age ;}}// compared with the person class, the person class function is extended. Therefore, class student extends person {private string school; Public student (string name, int age, string School) {// super (); // --> implicit code // It is better to explicitly specify the constructor that calls two parameters in the parent class. // directly specify the constructor that calls two parameters in the parent class. Super (name, age); this. setschool (school);} public void setschool (string School) {This. school = school;} Public String getschool () {return this. school;} // The subclass overwrites the getinfo () method Public String getinfo () {// call the getinfo () method in the parent class and use super. return super. getinfo () + ", school =" + this. school ;}} public class demo02 {public static void main (string ARGs []) {// use the subclass object student s = new student ("newsainton", 23, "Software Institute"); system. out. println (S. getinfo ());}}