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.
To be blunt, it is to write a method with the same name, the same number of parameters, the same return value, or a property of the same type, as the parent class, in the subclass. When the subclass object calls the method/property. A method that runs a subclass. 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 first. Then continue to run the subclass code.
)
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 have no constructors or no parameter constructors. The constructor of the parent class does not have to be called explicitly in the subclass constructor, and the constructor of the parent class is called on its own initiative 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.
A subclass can not invoke the constructor of the parent class in a constructor, and a non-parameter constructor for 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, and can alter the modifiers of the method. But only the scope of the method can be enlarged. 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 the subclass. Static methods with the same name as the parent class do not affect each other. Because a static method is called with a class name, the subclass's method is called using the subclass class name. Invokes the parent class's method 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 the child class 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 subclass or parent class uses an instance variable, it is equivalent to adding a this pointer (this.) in front.
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 subclasses override methods of the parent class. Methods for using subclass overrides //Conclusion two: When a subclass overrides an instance variable of the parent class, the parent class method uses the 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