9th one of the three main features of Java class: inheritance

Source: Internet
Author: User

1. What is inheritance

A subclass inherits a parent class, which is an extension to the parent class that automatically owns all the members outside of the parent class.
Role: Add code Reuse
Syntax format:

Class subclass name extends parent class name {
............
Chapter 9th one of the three main features of Java classes: inheritance
Example:

1 //test classes, create subclass objects, and call properties and methods directly2  Public classTestdog {3      Public Static voidMain (string[] args) {4Dog dog =NewDog ();5dog.age=10;6Dog.name= "Wang Choi";7 dog.eat ();8     }9 }Ten  One //subclass, without writing any method A  Public classDogextendsAnimal { -  - } the  - //test classes, create subclass objects, and call properties and methods directly -  Public classTestdog { -      Public Static voidMain (string[] args) { +Dog dog =NewDog (); -dog.age=10; +Dog.name= "Wang Choi"; A dog.eat (); at     } - } -  -  - ........... .......... -Animals have the ability to eat.

2. Overriding the methods in the inheritance

After inheriting the parent class, the subclass can override this method and prioritize the method of calling the subclass, the nearest principle.
A method override is a redefinition of a method that exists in the parent class at the time of inheritance, and overloading refers to the name of several methods in a class that are not the same, and that the corresponding method is called only when different parameters are received.
There are some limitations to the override of methods when inheriting:
1. Return value type
2. Method name
3. Parameter type and number
The above three is the same as the method inherited by the parent class, so it is called the override of a method
Example:

1 //Parent Class2  Public classAnimal {3      PublicString name;4      Public voideat () {5System.out.println ("The Animal" +name+ "can eat.");6     }7 }8 9 //subclass, overriding the Eat () methodTen  Public classDogextendsAnimal { One  A      Public voideat () { -System.out.println ("Here is the method overridden in a subclass"); -     } the } -  -  - //test classes, create subclass objects, and call properties and methods directly +  Public classTestdog { -      Public Static voidMain (string[] args) { +Dog dog =NewDog (); ADog.name= "Wang Choi"; at dog.eat (); -     } - } -  - ... ......... -Here is a method that is overridden in a subclass

3. Inheritance Initialization order

Initialization Order of Inheritance:
1. When you create an object when you are using a class, do you initialize the child class or initialize the parent class first when you create the subclass object?

The answer is to initialize the parent class first and then initialize the subclass

This can be done by printing the statements in the parent and subclass constructors, which must have been printed by the parent class first.
2. Since the parent class is initialized before the subclass, is it first initialized or first constructed?

The answer is to initialize the property first, and then initialize the constructor method

Summary: When you create a subclass object, initialize the properties of the parent class, initialize the parent class construction method, initialize the subclass property, and then the subclass constructs the method

4. Use of final in parent class

Final keyword:
Use final keyword to identify "final" meaning
Final can be decorated with classes, methods, properties, variables
If the class is modified: The class is not allowed to be inherited
If modified: The method is not allowed to be overridden (overwritten)
If a property is modified: The property of the class is not implicitly initialized (the class's initialization property must have a value) or is assigned in the constructor method (but only one is selected)
If the variable is modified: Then the value of the variable can only be assigned at the time it is defined, which becomes a constant

5. Use of super in sub-category

Super Keyword:

Used inside an object, you can represent the parent class object

1. Accessing the properties of the parent class

Super.age

2. Methods to access the parent class

Super.eat ()

Attention:
The properties of the parent class object are not related to the properties of the subclass object, and are two properties

The relationship between super and the construction method:
1. The constructor of the parent class must be called during the construction of the subclass
For example, when you create a subclass object, the constructor of the parent class is executed, rather than implicitly writing a super in the constructor of the subclass.

1 //Parent Class2  Public classAnimal {3      PublicAnimal () {4System.out.println ("The parent class has executed");5     }6 7 }8 9 //sub-classTen  Public classDogextendsAnimal { One      PublicDog () { A         //Subclass constructor Method, where you implicitly write a super -SYSTEM.OUT.PRINTLN ("Subclass executed")); -     } the  - } -  - //test the class, create the child class object +  Public classTestdog { -      Public Static voidMain (string[] args) { +Dog dog =NewDog ();//To create a child class object A  at     } - } -  - ... ......... - the parent class executed the -Subclass Executes the

2. If the constructor method for calling the parent class is not displayed in the constructor of the subclass, the system defaults to calling the parent class without a parameter constructor.

3. If the calling constructor method is displayed, the first line of the constructor method must be in the subclass, which shows the call with super ();

4. If the subclass constructor method does not show the constructor method that called the parent class, and the parent class does not have a parameterless constructor, the compilation error occurs.

Explanation: The parent class has no parameter construction method by default, and if you define a constructor method that has a parameter, no argument construction method is added automatically. In the subclass construction method, if there is no super (), the default is to call only the parent class's parameterless construction method, if you want to call the parent class's argument construction method, you only need the super (parameter);

Example:

1 //Parent Class2  Public classAnimal {3      Public intAge ;4      PublicAnimal (intAge ) {5          This. age=Age ;6System.out.println ("Parent class executed" + This. age);7     }8 9 }Ten  One //sub-class A  Public classDogextendsAnimal { -      PublicDog () { -         Super(10); theSYSTEM.OUT.PRINTLN ("Subclass executed")); -     } -  - } +  - //test the class, create the child class object +  Public classTestdog { A      Public Static voidMain (string[] args) { atDog dog =NewDog (); -  -     } -}

Summing up, actually said so much on one meaning:

If the parent class has no parameter construction method, super () write does not matter, if there is no parameterless construction method, you need to write super (), and super () pass parameters

The object class of 6.Java

The 1.Object class is the parent class of all classes, and if a class does not use extends, the keyword explicitly identifies inheriting another class, then the class inherits the object by default.
Methods in the 2.Object class, suitable for all subclasses
Several methods in the 3.Object class:
ToString () Method:
The ToString method defined inside the object class is the hash code of the returned object (object address string)
If the object name is output directly, the Tostirng () method is executed, for example:

System.out.println (Instantiate object name)

You can output an object's properties by overriding the ToString () method in a subclass

 Public String toString () {    return "Dog[age" +age+ "]";}

Equals () method
The source of the comparison is whether the object reference to the unified block memory address,
A subclass can be modified to compare the values of two objects for equality. This can be modified:

1 //Parent Class2  Public classAnimal {3      Public intAge ;4 5 6 }7 8 //sub-class9  Public classDogextendsAnimal {Ten @Override One      Public Booleanequals (Object obj) { A         if( This==obj)//determine if the referenced address is the same -             return true; -         if(obj==NULL)//determine if the comparison value is not a null value the             return false; -         if(GetClass ()!=obj.getclass ())//to compare code information for two object classes -             return false; -Dog other =(Dog) obj; +         if(age! = other.age)//determines whether the property values of two objects are equal -             return false; +         return true; A     } at } -  -  - //test the class, create the child class object -  Public classTestdog { -      Public Static voidMain (string[] args) { inDog dog =NewDog (); -Dog dog2 =NewDog (); to         if(Dog.equals (dog2)) { +System.out.println ("Two objects are the same"); -}Else{ theSystem.out.println ("Two objects are not the same"); *         } $ Panax Notoginseng     } -}

9th one of the three main features of Java class: inheritance

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.