Java Experience---Inheritance

Source: Internet
Author: User

1. Inheritance is one of the three main characteristics of object-oriented, and also an important means to realize software reuse.

¯ inherited features (the keyword is extends,extends English meaning is extended, not inherited. But this keyword is a good embodiment of the subclass and the parent class relationship, that is, the subclass is the parent class inheritance, is a special parent class

Μ through inheritance, on the basis of existing types of expansion or transformation, to obtain new data types.

The type you already have is called a parent class, base class, or superclass.

You get the new data type, called subclasses or derived classes

The inheritance of the class improves the reusability and extensibility of program code, and shortens the cycle of software development.

2. Classification of inheritance:
Single inheritance---------subclasses can have only one direct parent class
Multiple inheritance------Subclasses can have multiple direct parent classes

Java does not support multiple inheritance, not that Java classes can have only one parent class, but that Java classes can have only one direct parent class, in fact Java classes may have infinitely many indirect parent classes, is-a represents an inheritance relationship (is a), and has a represents an aggregation relationship (one). Java subclasses cannot directly invoke the constructor method of the parent class.

3. Rules to follow when method overrides : "Three same small one big" rule

1)." Three is the same as the method name, the parameter list is the same, the return value type is the same;

2). " A small "means that the exception class that the subclass method declaration throws should be smaller or more equal than the exception class thrown by the parent class method declaration;

3). " A large "means that the access rights of the subclass method should be greater or equal than the parent class method;

4. Coverage methods and overridden methods are either class methods or instance methods, not a class method, or an instance method.

4. If you define a Java class that does not display a direct parent class that specifies this class, the class defaults to the extended Java.lang.Object class. Java.lang.Object is the parent class of all classes, either a direct parent class or an indirect parent class. All Java objects can invoke instance methods defined by the Java.lang.Object class. The object class is the root class of the class hierarchy. So a class can call a method in object directly without declaring it and inheriting it.

Subclass Extension (extender) parent class, parent class derivation (derive) out subclass, extension and derivation describe the same action, but the angle of observation is different.

5. The difference between method overrides (override) and method overloads (overload)

1. Overrides are the relationships between subclasses and parent classes, while overloading is the relationship between multiple methods within the same class. (A subclass that contains a method with the same name as a parent class is called a method override or method overlay.) )

2. Overrides are typically between two methods, while overloading may have multiple overloaded methods.

3. The overridden method has the same method name and formal parameter list, whereas overloaded methods can have the same method name and cannot have the same formal parameter list.

4). When overwriting, the method is differentiated according to the object that called it, and the overload is based on the formal parameter list to determine which method is invoked.

6. Super Reference to parent class instance

When the subclass method overrides the parent class method, the the object of the subclass will not be able to access the overridden method in the parent class, but you can still call the overridden method in the parent class in the subclass method, and if you need to call the instance method overridden by the parent class in a subclass method, you can use the super As the caller to invoke the instance method overridden by the parent class.

Attention:

1. Super is a Java-supplied keyword that is the default reference for a direct parent class object.

2. As this cannot appear in a static-decorated method, super cannot appear in a static method; The static-decorated method belongs to the class, and the caller of the method may be a class, not an object, and there is no corresponding parent object. So the super reference loses its meaning. This, private as object, instance, must first new object before it can be used. Super (); this (); Only the first statement of the method is constructed, and both cannot appear at the same time (because all are in the first place), and in other places the super. Object name, this. Object name.

3. If the subclass defines a property that has the same name as the parent class, the child class property overrides the parent class property. When a subclass's method accesses the property directly, it accesses the Override property, cannot access the property overridden by the parent class---access the overridden properties of the parent class through Super

4. If the parent class method has private access, the method is hidden from its subclass and cannot be overridden by subclasses that cannot override the method, even if a subclass defines a method with the same name, the same parameter list, and the same return value type as the private method of the parent class. Still not rewrite.  However, you can use the set, Get methods to invoke (access between two different classes can also do so); For example, there is a private int a in the parent class A; You can do the set, get function to invoke, Publicvoid seta (int a) {this.a = A;} Public Intgeta () {return this.a;}; You can call Seta directly in subclass B and assign a value to it, B = new B (); B.seta (10).

7. If the class attribute is overridden, the overridden class property can be invoked through the parent class name in the subclass's method, and the subclass inherits directly to the parent class property if there is no property in the subclass that has the same name as the parent class. If you access this property in a subclass instance, you do not need to use the Suoper or the parent class masterpiece as the caller.

If we access a property named a in a method but do not display the specified caller, the system looks up the order of a:

• Find local variables that are known as a in this method

U find whether the current class contains a property named a

Find out whether the direct parent of a has a property named A, which traces the parent class of a, until the Java.lang.Object class, and if the property named A is eventually not found, a compilation error is present on the system.

8. Subclasses do not get a constructor for the parent class. But sometimes the subclass constructs the method to invoke the initialization code of the parent class construction method, the method that invokes another overloaded construct in one constructor, is implemented by using this, and the parent class construction method is called in the subclass construction method, which is generally implemented using super.

Note: The super call is much like the this call, except that super calls the constructor of its parent class, and this calls the constructor method of the overload in the same class. Therefore, calling the parent class construct with super must also appear in the first row of the subclass construction body, so the this call and the super call do not appear at the same time .

9. Whether or not the super call is used to execute the initialization code of the parent class construction method, the subclass constructor always calls the parent class's construction method once.

The subclass constructor method calls the parent class construct method in several cases as follows:

The first line of code for the Μ subclass constructor method invokes the parent class constructor using the super display, which calls the corresponding constructor of the parent class based on the argument list passed in to the super call.

Μ the first line of the execution body of a subclass constructor uses this to display calls to the overloaded constructor in this class, and another constructor of this class is called based on the argument list passed in in this call. The constructor method of the parent class is invoked when another construct method in this class is executed.

There is neither a super call nor this invocation in the Μ subclass constructor execution body, and the system will implicitly call the parent class parameterless construction method before executing the subclass construction method.

Regardless of the above, when the parent class constructor is invoked to initialize the subclass object, the parent class constructor always executes before the subclass constructs the method.

Note: When you create a subclass object from a subclass constructor, the default is to execute the parent class constructor without parameters, and then the subclass class constructor method.

10. Polymorphism means that a method with the same name may have multiple versions in the program, the user uses the same method name to invoke the method, the system will invoke the different versions of the method according to the specific situation, so as to achieve different functions, and implement "one interface, multiple methods."

Polymorphism allows the processing of existing variables and related classes in a unified style, making it easy to add new functionality to the system, and inheritance and polymorphism are effective technologies for reducing software complexity; polymorphism technology mainly manifests in the method overload and the method covers two aspects. The inherited permissions do not change.

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.