Inheritance of three major features of object-oriented (extends)--java notes (vi)

Source: Internet
Author: User
Tags export class

Inheritance: From general to special relationship, is a kind of extension relations, sub-class object is a kind of parent class, also can be called "is a" relation generalization: the common character of the class is extracted into the parent class of the process specialization: subclass on the basis of the parent class to define their own unique behavior characteristics of the process Format: Modifier class Subclass extends superclass{} Subclass: Export class, inheriting class, subclass superclass: Parent class, base class, superclass, source class subclass subclass inherit parent class by extends Superclass inheritance is throughextendsTo achieve theinheritance improves the reusability of code and improves the efficiency of software development . to have a relationship between classes and classes is the precondition for polymorphismWhen a subclass object accesses an instance member, it is now found in the subclass, and if it is not found, go to the parent class and find each class with only onedirectlyThe parent class does not show the inheritance of another class when the default direct parent class is the object class, and once a class is shown inheriting another class, the default direct parent object is canceledJava only supports single inheritance and does not support multiple inheritanceSomething private inside a parent class, a subclass cannot inherit in an inheritance operation, and for an object instantiation of a subclass, the subclass object must beConstructor method that calls itself after the constructor method in the parent class is called firstSub-Certificate:
1 classsuper{2   PublicSuper () {3System.out.println ("I am a super parent");4  }5 }6 classSubextendssuper{7   PublicSub () {8System.out.println ("I am sub-class sub");9  }Ten } One  Public classInstancedemo { A   Public Static voidMain (string[] args) { -   NewSub (); -   /** the * I'm a parent super - * I'm a sub-class sub -    * - * The first print is inside the parent class inside the printed sub-class + * from this you can see that the constructor of the parent class is called first to create the parent class object and then to call the constructor inside the subclass. -    * +    */ A  } at}
The subclass object will default to calling the parent class's parameterless constructor before initialization, but once the subclass shows a call to the other constructor of the parent class, the constructor of the default call will be overridden by the method's override (override): When a method of the parent class is not suitable for the characteristic behavior of the subclass itself, it is Write a method that should be changed in the parent class the overriding principle should be followed: The method signature (method name + parameter list) must be the same as the subclass method's return value type is smaller than the return value type of the parent class method or the exception that is thrown by the equality subclass method declaration should be less or equal than the parent class method assertion, that is, the , a subclass method can simultaneously declare that the access permission of a subclass (RuntimeException exception) subclass method that throws more than one parent class method declaration throws an exception should be greater or equal than the parent class method overload and override

Distinguishing points Overloading (overload) Override (Override)
1 Judging rules The parameter list order is different, the type is different, the number is different can form the overload The method name is the same in the same class, but the parameter list of the method is different, regardless of the method's return value type and modifier method Signature (method name + parameter list) must be the same The return value type of a subclass method is smaller or equal than the return value type of the parent class method the subclass method declaration throws an exception that is smaller or equal than the parent class method statement. subclass methods should have greater or equal access to the parent class method
2 Permissions No permission required The overridden method cannot have more restrictive permissions than the parent class
3 Range Occurs in a class Occurs in an inheritance relationship
4 Polymorphic Compile-time polymorphism Run-time polymorphism
Construction method cannot be overriddenSuper keywordRepresents the default reference for a parent class object if the subclass is to invoke an instance method that the parent class is overridden, use super as the caller of the parent class to invoke the overridden instance method using Super call to the parent class method using super Call the constructor method of the parent class for super calls must be the first statement in the constructor this cannot In the static modified method, Super also cannot appear in the static decorated method because the static decorated method belongs to the class, that is, the caller is the classThis and Super
difference                This Super
1 Use Call a field or method in this class Calling a field or method of a parent class from a subclass
2 Constructor The constructor method in this class can be called, and there is a construction method to be used as an exit The constructor of the parent class is called from the subclass, regardless of how the subclass is scheduled to eventually be called, the default call to the parent class without a parameter in the constructor method
3 Requirements Place the first row of the constructor method when invoking its own construction method The first row of the child class construction method is placed when the parent class constructor method is called
4 Special Represents the current object Represents the parent class object
PS: Using super () and this () in the same construction method is not possible to display the occurrence of

Inheritance of three major features of object-oriented (extends)--java notes (vi)

Related Article

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.