Rewrite
Meaning
In Java, a subclass can inherit methods from its parent without having to rewrite the same methods. But sometimes the subclass does not want to inherit the method of the parent class intact, but wants to make some changes, which requires the method rewriting. Method override is also called method override.
Similarities and differences between rewriting and overloading
Same points
It's all about methods.
Difference
When overloaded, it works in the same class. Multiple methods have the same name and different parameter lists (with different method signatures).
Rewriting works on different classes, which inherit from the parent class that needs to modify the method.
Difference:
Overload is in the same class, with the same method name and different parameter list.
Rewriting is used in two subclasses with inheritance relationship, and the method signature is the same
Why rewriting is needed
In some business implementation processes, the subclass has the function inherited from the parent class, but the function to be implemented by the subclass is different from that of the parent class, so we need to rewrite this function to meet our business needs
Override (can only be overridden in a subclass)
Overridden method: same as the normal method, but the method name and parameter list should be the same as in the parent class
Call order of overridden methods
The use of subclass objects in calling this function. If there is an override method, call the override method. If there is no override method, call the method of the parent class.
public class OverrideDemo { public static void main(String[] args) { //Create an instance (object) of a subclass Son son=new Son(); //Overloaded methods using subclasses son.word(); } } //Parent class class Dad{ private int money=1250; //The method of the parent class cannot be inherited by the child class if static is added public void words(){ System.out.println("I'm the parent method"); } } //Subclass parent class inherits subclass class Son extends Dad{ //After the parent class is modified by static, the method does not override the method of the parent class, but creates a new method. public void word(){ System.out.println("I'm a subclass method"); } }
**Note: * * in the parent class, if the method is modified by static, the child class cannot inherit it. If the child class has the same method signature, the method is a new method of the child class, not an override of the parent class method.
super keyword
The difference between super and this
this: refers to the currently created object
- Call other constructors in this class
- Must be placed on the first line of the constructor
- this([parameter list]) / / the parameter is that the list matches other constructors in this class.
- The problem of distinguishing variables with the same name (local variable and member variable)
- this. The variable name refers to the variable of the currently created object, that is, the member variable.
super: refers to the superclass object
- Call the parent constructor
- The default is to call the empty constructor of the parent class / / so when writing JavaBeans, an empty constructor is required.
- Other constructors calling the parent class need to explicitly call super([parameter list]), and the parameter list matches the parameter list of the parent class constructor
- Same as this, it must be placed on the first line of the constructor
- this and super are in the same constructor, cannot call other constructors at the same time
- Distinguishing variables of the same name (subclass and superclass)
- When a member of a local subclass has the same name as a member of its parent class, the local variable is called to write the variable name | method name (), and when a subclass variable is called to write this. Variable name | this. Method name (), and when a member of its parent class is called to write the super. Variable name | super. Method name ()
- Note: the call construction method is different from the call ordinary method. Use super. Variable name | super (parameter list)
There are two ways to set attribute values in a parent class:
- Use setter and accessor to set and access properties in the parent class
- When a subclass creates an object, it calls the subclass constructor, and the subclass constructor. In the first line of the subclass constructor, we can call the parent constructor to pass in the parameters, and then pass the parameters to the parent constructor to realize the assignment of the parent properties.
public class Supper01 { public static void main(String[] args) { //Execute the nonparametric construction of the subclass first, and the nonparametric construction of the parent class Son son1=new Son(); //First, execute the parameter construction of the subclass, and then pass the value to the parameter construction of the parent class. Son son2=new Son("Xiao Wu"); } } //Parent class class Dad{ //Parent member variable String name; //Parent member method public void study(){ System.out.println("Father is studying"); } //Parent constructor public Dad() { System.out.println("Parent empty construction"); } public Dad(String name) { this.name = name; System.out.println("Parent structure with parameters"+this.name);//The name here refers to the member of the current parent class } } //Subclass inherits parent class Son extends Dad{ //Subclass member variable String name; //Subclass member method @Override public void study() { System.out.println("My son is learning.Covering my father's learning"); } //Subclass constructor 1 public Son() { System.out.println("Subclass parameterless constructor"); } //Subclass constructor 2 public Son(String name) { super(name);//The construction of passing variables to the parent class System.out.println("Subclass with parameter constructor"); } }
Final keyword final
Remember the following three points:
-
Variables decorated with final are constants
- If the data type of the variable is the basic data type, the stored data value cannot be changed.
- If the data type of a variable is a reference data type, the stored address value will not change, but the properties in the object memory can change.
-
Methods decorated with final cannot be overridden
public class Final01 { public static void main(String[] args) { DadOne dad=new DadOne(); SonOne son=new SonOne(); son.eat(); } } class DadOne{ int a=5; public static void eat(){ System.out.println("Parent eating"); } } class SonOne extends DadOne{ public static void eat(){//The signature of the method is the same as that of the parent class, but it is not overridden, because the method has been final ly modified in the parent class, which is a new method. System.out.println("Subclass eating"); } }
The above code will not report an error.
-
The class modified by final cannot be inherited (commonly known as eunuch class)
public class Final01 { public static void main(String[] args) { DadOne dad=new DadOne(); SonOne son=new SonOne(); } } final class DadOne{ int a=5; } class SonOne extends DadOne{//This trip will report an error }
Object class (the most basic class of all classes, equivalent to the source)
So the father of the class, all classes in java will inherit directly or indirectly from the Object class.
If there is no explicit inheritance of other classes, the default inheritance is from the Object class.
toString method
-
toString() displays the contents of an object as a string
Source code:
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }
When printing the reference of an object, print the address of the object. By default, this object calls the return value of toString method, which can be overridden as required.
-
equal method:
equals
- If you use the equal() method in the Object class, the comparison is the address of the Object.
- If the equal() method is overridden, the comparison is the content of the method override. For example, String.euqals("");
/* Judge whether two users are the same user, judge whether the user name and password are the same, and output the user information. Idea: Rewrite equals method, judge user name and password, rewrite toString to output user information */public class UserNamePassword { public static void main(String[] args) { UserPassword u1=new UserPassword("Little Weng","123456"); UserPassword u2=new UserPassword("xaiaong","56152"); UserPassword u3=new UserPassword("Little Weng","123456"); String a="525"; System.out.println(u1); System.out.println(u2); System.out.println(u3); System.out.println(u1.equals(u2)); System.out.println(u1.equals(u3)); System.out.println(u1.equals(525)); System.out.println(u1.user.equals("Little Weng")); } } class UserPassword{ public String user; public String password; public UserPassword() { super(); } public UserPassword(String user, String password) { super(); this.user = user; this.password = password; } //Override toString method @Override public String toString() { return user+password; } //Override euqual method @Override public boolean equals(Object obj) { if(this==obj){ return true; }else{ if(obj instanceof UserPassword){ UserPassword u=(UserPassword)obj; if(this.user==u.user){ return true; }else{ return false; } } return false; } } }
polymorphic
Three characteristics of object-oriented
Encapsulation: hide the internal implementation details and provide public methods. Private is a specific form of encapsulation.
Inheritance: once a subclass inherits the parent, it has the right to implement the content of the parent.
Polymorphism: a variety of forms and manifestations of a thing
The premise of polymorphism
Must be an inheritance of a class or an implementation of an interface
The ultimate manifestation of polymorphism
A reference to a parent class points to an object of a child class
Suppose Person is the parent class and Study is the child class
Person p=new Study();
Call order of polymorphic rewriting method
If there are overriding methods in both the subclass and the parent class, polymorphic call is the overriding method in the subclass. If not, directly call the method of the parent class.
Note: the parent class reference is not visible to the new content of the child class
Polymorphic compilation and operation
- Member variables
- Compile to see parent class and left to see type
- Member method
- Compile to see the parent class, run to see the child class
- Compile to the left, run to the right
- Compile to see type, run to find object
public class Shy { public static void main(String[] args) { Pig pigOne=new Pig(); Aniaml pigTwo=new Pig(); //test //Subclass references to subclass object member variables and member methods can be used System.out.println(pigOne.name); pigTwo.eat(); /*Parent class reference points to child class object Member variable: properties of subclass objects cannot be used directly Member method: if there is a subclass override method, directly use the subclass override method If the method does not exist in the subclass and exists in the parent, the method of the parent is used directly Note: if the variable cannot use the new content in the subclass */ System.out.println(pigTwo.name+"==="); // System.out.println(pigTwo.name); pigTwo.eat(); } } class Aniaml{ //Member variables public String name="Animal"; private int age; //Member method public void eat(){ System.out.println("I love eating"); } //Constructor public Aniaml() { super(); } public Aniaml(String name, int age) { super(); this.name = name; this.age = age; } } //Subclass class Pig extends Aniaml{ String name="Piglet"; int age; public Pig(){ super(); } public Pig(String name,int age){ super(name,age); } @Override public void eat() { System.out.println("Pigs eat pigs"); } }
Matters needing attention
Subclass references point to subclass objects. Member variables and member methods can take the following forms:
//Person is the parent class and Study is the child class Person p=new Study();
Member variable: properties of subclass objects cannot be used directly
Member method: if there is a subclass override method, directly use the subclass override method
If the method does not exist in the subclass and exists in the parent, the method of the parent is used directly
**Note: * * if the variable cannot use the new content in the subclass
Subtype conversion
Up type conversion
Is the use of polymorphism.
Down type conversion
Subclass type reference = (subclass type) data of parent type;
Judge whether it is a child of the parent class, and use instanceof to judge
Note: after the downward transformation, you can use the new content of the subclass.
public class Shy02 { public static void main(String[] args) { //Confucius shows his father's face //The reference of the father class (Confucius father) points to the child class object (Confucius) //Transition up from child to parent ConfuciusDad kzd=new Confucius(); kzd.teache(); //In order to use the new content of a subclass, you need to make a downward transformation and the parent class changes to a subclass. Confucius kz=(Confucius)kzd; kz.play(); //In the downward transformation, the parent class reference can be transferred to any subclass type, but it may not be what we want. /* Brother kzb=(Brother)kzd; kzb.play(); */ //Before downward type conversion, we can use instanceof to determine whether it is the subclass we want. if(kzd instanceof Confucius){//Judge whether kzd is an instance (object) of conflucius and return boolean value Confucius kzz=(Confucius)kzd; kzz.play(); } } } class ConfuciusDad{ public void teache(){ System.out.println("Kong Zi Dad---Teaching"); } } class Confucius extends ConfuciusDad{ public void teache(){ System.out.println("Confucius---Teaching"); } public void play(){ System.out.println("Big talk Westward Journey"); } } class Brother extends ConfuciusDad{ public void teache(){ System.out.println("Confucius brothers---Teaching"); } public void play(){ System.out.println("LOL"); } }
abstract
Abstract class: class decorated by abstract
Abstract methods: methods not modified by abstract
Abstract method: must be in an abstract class and must be in an abstract class
Be careful:
- Abstract class cannot be instantiated | object cannot be created
- Abstract methods must be rewritten in order to exist in abstract classes
- Use of abstract classes
- Concrete subclass: override the abstract method used + add on demand
- Abstract subclass: Rewrite abstract methods on demand + add on demand
- Once an abstract method is rewritten, it does not need to be rewritten again
- There can be ordinary methods and abstract methods in abstract columns
- abstract cannot be used with private,final,static,native
Parent class:
- Specific parent class
- You can create a parent class object and know how the content in the parent class is defined. You can define a specific parent class.
- Abstract parent class
- Don't want to be able to create parent objects directly
- The method body in the parent class does not know how to implement
Example:
Defining abstract classes
//Define an abstract class public abstract class Develop { //Define two abstract classes work sleep public abstract void work(); public abstract void slepp(); }
Define specific subclasses
//The concrete subclass must implement all the methods in the inherited abstract class public class Java extends Develop{ @Override public void work() { System.out.println("java The siege lion is working"); } @Override public void slepp() { System.out.println("java The siege lion is sleeping"); } public void huo(){ System.out.println("java City Lions are taking private jobs"); } }
Define abstract parent class and concrete child class
//The concrete subclass must implement all the methods in the inherited abstract class //If there is one that is not implemented, the class should also be defined as an abstract class public abstract class Web extends Develop{ @Override public void work() { System.out.println("Web The engineer is working"); } } class Demo extends Web{ //Implemented the above unimplemented method @Override public void slepp() { System.out.println("web The engineers are resting"); } }
Test code
public class Test { public static void main(String[] args) { Java java=new Java(); java.work(); java.slepp(); java.huo(); Demo d=new Demo(); d.slepp(); d.work(); } }