Chapter 3 inheritance in the second quarter of Java

Source: Internet
Author: User

Chapter 3 inheritance in the second quarter of Java
3-1 Inheritance in Java ★Concept: inheritance is a kind of relationship between classes and classes. It is a kind of "is a" relationship. Figure: inheritance relationship-dogs and animals Note: in Java, inheritance is a single inheritance, that is, a class has only one parent class. ★BenefitsSubclass has all attributes and methods of the parent class,However, if private modifier exists, the subclass cannot use this attribute or method.Code reuse ★Syntax RulesClass subclass extends parent classExample: class Dog extends Animal {......}In Eclipse, you can select the parent class in Superclass when creating a class.Automatic Generation:3-2 method rewriting in Java ★ConceptIf the subclass is not satisfied with the method that inherits the parent class, you can override the method inherited by the parent class. When you call a method, the subclass method is preferentially called.★Syntax RulesA. Return Value Type B. method name c. Parameter type and quantityMust be the same as the method inherited by the parent class., CalledMethod Rewriting ★Example: Dogs and animals Figure: parent-animalFigure: subclass-dogFigure: Test class Running result:Age: 1. A dog can eat.3-3 inheritance initialization sequence in Java Inherited initialization sequenceA. initialize the parent class before initializing the subclass.B. First execute the attributes in the initialization object, and then execute the initialization in the constructor. ★Instance 1: Verify that the parent class is initialized before the subclass is initialized. Figure: parent-animalFigure: subclass-dogFigure: Test class-call the eat () method Running result: ★Example 2: Verify that the attributes in the initialization object are executed before the initialization in the constructor. Figure: parent-animalFigure: Test class-call the eat () method Running result: ★Summary3-4 Use of final in Java The final keyword is used as the identifier and has the "final" meaning, that is, it cannot be modified and cannot be changed. Final can modify classes, methods, attributes, and variables.Final modifier class --This classInheritedFinal modification method --This methodCannot be overwritten(Rewrite)Final modifier attribute -- attributes of this class are not implicitly initialized (class initialization attributes must have values), or assigned values in the constructor (but only one of them can be selected)The attribute value cannot be modified elsewhere.Final modifier variable -- the value of this variable can only be assigned once, that is, changed to a constant 3-5 Use of super in Java Super Keyword: used inside the object, which can represent the parent class ObjectAccess the attributes of the parent classSuper. ageMethod for accessing the parent classSuper. eat () The constructor of the parent class must be called during subclass construction. This process implicitly uses the super keyword.If the constructor of the Child class does not display the constructor of the parent class, the system calls the constructor of the parent class without parameters by default. If you call the constructor explicitly, it must be in the first line of the constructor of the subclass.Figure: Example -- explicitly calling the constructor of the parent class If the subclass constructor does not display the constructor that calls the parent class, and the parent class does not have a constructor without parameters, the compilation fails. Figure: parent class Animal (No parameter-free constructor is declared)Figure: subclass Dog (error)Figure: error message 3-6 Object classes in Java The Object class is the parent class of all classes.If a class does not use the extends keyword to explicitly identify the inheritance of another class, this class inherits the Object class by default. Methods In the Object class, suitable for all subclasses.★ToString () method● When defining the toString () method in the Object class, return the hash code of the Object (the Object address string-uniquely identifies the Object) ● you can override toString () method to indicate the properties of the objectFigure: inheritance relationships between the three classes Example 1: Call the built-in toString () Running result:Note: imocc. Dog -- package name @ back -- Memory Address Example 2: Custom toString () method Step 1: Click Source and select Generate toString () Step 2: select the attribute you want to output and click OK. Figure: Generation and Running Effect ★Equals () methodCompare whether the object reference points to the same memory addressWhen comparing two objects,Compare whether their values are consistent, So rewriteExample 1: Call the. equals ()Note:. equasl () and "=" are both addresses by default. The running result is "two objects are different" Example 2: Custom toString () method Step 1: Click Source and select Generate hashCode () and equals () Step 2: select the attribute you want to output and click OK. Figure: generated code ▲Figure: differences between class objects and class objects 

 

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.