Click to enter _ many other _java thousand ask
1. What is coverage
In Java, overrides are the concept of inheritance, and a subclass requires certain methods or properties. However, you do not want to use a method or property of the same name in the parent class. You need to use overrides.
It is straightforward to write a method in a subclass that has the same name, the same number of parameters, and the same return value as the parent class. Or a property of the same type with the same name. When a subclass object calls the method/property, it runs a method of the subclass without running the parent class's method (unless you write Super () on the first line of the method) and the parent class method runs 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. You need to call the constructor of the parent class when constructing the subclass. There are three kinds of situations
- 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 that invokes the parent class in the constructor method. Otherwise, a compilation error occurs.
- 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. Must have the same number of parameters and return type.
- Subclasses override methods of the parent class, after jdk1.5, the number of parameters, the return type, can be a subclass of the parent class's method return class.
- Subclasses override the methods of the parent class, can alter the modifier of the method, but only enlarge the scope of the method, and cannot change the 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.
- Polymorphic. When a subclass overrides a method of the parent class. Use the methods covered by subclasses.
Understand what polymorphism is: [What is the performance of Java polymorphism][6]
[6]:
4. How attributes are overwritten
- When a child class overrides an instance variable of the parent class, the parent class method uses the instance variable of the parent class. A subclass method uses an instance variable of a subclass.
- 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, use the subclass override method //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