Click to enter _ many other _java thousand ask
1. What is coverage
In Java, overrides are the concept of inheritance, where a subclass needs some method or property, but does not want to use a method or property of the same name in the parent class.
In a straightforward sense. is to write a method in a subclass that has the same name as the parent, the same number of references, the same return value, or a property of the same type, and the subclass object is running the subclass when it calls the method/property. Instead of running the parent class's method (unless you write Super () on the first line of the method), the parent class method is run before the subclass code is run. )
Learn about the constructors of classes here: What are the secrets of classes and objects?
Learn a lot about other inheritance see here: what is the meaning of Java class inheritance?
2. How to override the constructor function
Learn about the constructors of classes here: What are the secrets of classes and objects?
When a subclass inherits a parent class, the constructor of the parent class needs to be called when the subclass is constructed, there are three cases
- When the parent class has no constructors or a non-parameter constructor. Subclasses do not need to explicitly invoke the constructor of the parent class if there is no constructor or no parameter constructor in the subclass constructor function. The system makes its own initiative to call the constructor of the parent class before calling the subclass constructor.
- When the parent class has only a parameter constructor. Subclasses must display the constructor of the calling parent class in the constructor method, or the compilation will fail.
- The parent class has neither a parameter constructor nor a constructor function.
Subclasses can not call the constructor of the parent class in the constructor. In this case, the non-parameter constructor of the parent class is used.
3, method how to cover
- Subclasses override methods of the parent class and must have the same number of parameters and return types.
- The subclass overrides the method of the parent class after jdk1.5. The number of parameters, the return type, can be a subclass of the class returned by the parent class method.
- Subclasses override methods of the parent class. Modifier that can alter the method, but only enlarges the scope of the method. Instead of changing public to private.
Learn a lot of other Java modifiers see here: What's the difference between public, privite, protected
- The subclass method can access the protected property of the parent class. However, you cannot access the default properties.
- The static method of a subclass is not affected by static methods with the same name as the parent class.
Because a static method is called with a class name, the method of the subclass is called using the subclass class name, and the parent class's method is called using the parent class name.
- Polymorphism, when subclasses override methods of the parent class, use the method overridden by the subclass.
Understand what polymorphism is: [What is the performance of Java polymorphism][6]
[6]:
4. How attributes are overwritten
- When a subclass overrides an instance variable of the parent class, the parent class method uses the instance variable of the parent class, and the subclass method uses the instance variable of the child class.
- When a child class or parent class uses an instance variable. is equivalent to adding a this pointer to the front (this.).
Learn a lot about other Java variables see here: What is the difference between local variables, class variables, and instance variables
5. Example
class superclass { Private intNumber PublicSuperclass () { This. Number =0; } PublicSuperclass (intNumber) { This. Number = number; } Public intGetNumber () {number++;returnNumber } } class SubClass1 extends superclass { PublicSubClass1 (intNumber) {Super(number); } } class SubClass2 extends superclass { Private intNumber PublicSubClass2 (intNumber) {Super(number); } } Public class subclass extends superclass { Private intNumber PublicSubclass (intNumber) {Super(number); } Public intGetNumber () {number++;returnNumber } Public Static voidMain (string[] args) {Superclass s =NewSubclass ( -); Superclass S1 =NewSubClass1 ( -); Superclass S2 =NewSubClass2 ( -); System.out.println (S.getnumber ()); System.out.println (S1.getnumber ()); System.out.println (S2.getnumber ());//Conclusion one: Polymorphic. When a subclass overrides a method of the parent class, use the method overridden by the subclass //Conclusion two: When a subclass overrides an instance variable of the parent class. The parent class method uses an instance variable of the parent class. Subclass methods Use instance variables of subclasses} }
Output Result:
1
21st
21st
Java Q _05 What is overridden in object-oriented (008) _java