Java thousand ask _05 object-oriented (008) _java What is covered

Source: Internet
Author: User

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

    1. 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.
    2. 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.
    3. 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
    1. Subclasses override methods of the parent class and must have the same number of parameters and return types.
    2. 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.

    3. 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

    4. The subclass method can access the protected property of the parent class. However, you cannot access the default properties.
    5. 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.

    6. 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
    1. 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.

    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.