class inheritance
Under the inheritance mechanism, the written class can be reused. If the subclass inherits the parent class, the subclass will have the member variables and member methods of the parent class, which reduces the redundancy of the code and increases the reusability of the code.
grammar:
class Parent { } class Child extends Parent { }
Extends is an inheritance keyword. Note that Java only supports single inheritance. A class can only have one parent class, but a class can have multiple subclasses
Inherited sample code:
class Parent { int Id; String name; public String toString() { //The toString method provides a specific output mode for the object, which is called by default when the object is printed by the Printf() method return Id+" "+name; } } class Child extends Parent { } public class Temporary { public static void main(String[] args) { Child child1 = new Child(); child1.Id = 1001; child1.name = "Jack"; System.out.println(child1); //output object information } } /*operation result: 1001 Jack */
For example, the Parent class has two member properties, Id and name, and the toString method, and the Child class inherits the Parent class, so it also has these two member properties and methods.
Construction method
Constructors are class methods that are called when an instantiated object is newly created. If there is no constructor in the class, the system automatically calls a default no-argument constructor. With the parameterized construction method, you can pass values for the member properties of the object when instantiating the object. If the class has a parameterless constructor, the system will not automatically call a default parameterless constructor, so it is a good practice for each class to write its own parameterless constructor
this keyword
The this keyword represents a reference to the current object that is calling the method. It is used in the first line of the constructor. For example, this.name represents the name property of the current object.
super keyword
super() means to call the constructor in the parent class, used in the first line of the subclass constructor, such as super(Id,name), which means to call the parent class without parameters, and transmit Id and name as parameters
The sample code is as follows:
class Parent { int Id; String name; public Parent () { } //no-argument constructor public Parent(int Id,String name) { //Argument constructor this.Id = Id; this.name = name; } public String toString() { //The toString method provides a specific output mode for the object, which is called by default when the object is printed by the Printf() method return Id+" "+name; } } class Child extends Parent { public Child() { } //no-argument constructor public Child(int Id,String name) { //Argument constructor super(Id,name); } } public class Extend { public static void main(String[] args) { Child child1 = new Child(); //Create an object with no-argument construction child1.Id = 1001; child1.name = "Jack"; System.out.println(child1); //output object information Child child2 = new Child(1002,"Tom"); //Create objects with parameterized constructs System.out.println(child2); //output object information } } /*operation result: 1001 Jack 1002 Tom */
method overriding
When the method inherited from the parent class cannot meet the needs of the subclass, the existing method in the parent class can be rewritten in the subclass, then the subclass object can call the method overridden by the subclass, realizing the expansion of the code sex
Sample code:
class Parent { int Id; String name; public Parent () { } //no-argument constructor public Parent(int Id,String name) { //Argument constructor this.Id = Id; this.name = name; } public String toString() { //Methods already in the parent class return Id+" "+name; } } class Child extends Parent { public Child() { } //no-argument constructor public Child(int Id,String name) { //Argument constructor super(Id,name); } @Override //Pseudo code, rewrite identifier, the function is to inform the compiler to check the rewrite method below public String toString() { //Overridden version in subclass return name+"of"+"Id"+"Yes"+Id; } } public class Extend { public static void main(String[] args) { Child child1 = new Child(); //Create an object with no-argument construction child1.Id = 1001; child1.name = "Jack"; System.out.println(child1); //output object information Child child2 = new Child(1002,"Tom"); //Create objects with parameterized constructs System.out.println(child2); //output object information } } /*operation result: Jack The Id is 1001 Tom The Id is 1002 */
Note that when a method is rewritten, the return type, method name, and parameter types of the method remain unchanged, and the access scope restricted by the modifier cannot be narrowed. Only the method body is rewritten.