Java,extends, inheritance

Source: Internet
Author: User
Tags class definition control characters

1. Concept of inheritance:

Inheritance is special--the general relationship. The subclass inherits the parent class, indicating that the subclass is a special parent class and has some properties or methods that the parent class does not have.

2. Initialization sequence in inheritance:

From the structure of a class, there are four common patterns within it: attributes (including class attributes and instance properties), methods (including class methods and instance methods), constructors, and initialization blocks (including initialization blocks of classes and initialization blocks of instances). For the initialization order in the inheritance, it also divides into the initialization of the class and the initialization of the object.

Class initialization:

During the preparation phase of the JVM load class, the memory space is first allocated for all class properties and class initialization blocks of the class. and initialized for it during the first initialization phase of the class, the order in which the class properties and class initialization blocks are defined determines the order in which they are initialized. If the class has a parent class, the class properties and class initialization blocks of the parent class are initialized first, beginning with the object class.

Object initialization:

When new creates an object, it first allocates memory for object properties and initialization blocks, and performs default initialization. If there is a parent class, first allocate memory and perform initialization for the parent class object and initialization block. The initializer in the parent class constructor is then executed before the object properties and initialization blocks of the child class are initialized.

Attention:

1. During the object initialization phase, both properties and methods are for properties and methods that subclasses can inherit from the parent class, generally in the case of non-private to the parent class.

Because the private adornment is unique to the parent class, the subclass does not inherit, and when the new subclass does not need to allocate space for it and perform initialization. Of course, the constructor subclass of the parent class is not inherited.

But the constructor is another matter.

2. Class initialization is performed only once, and when multiple objects are new to the same class, class properties and class initialization blocks are initialized only once.

3. Modifiers in inheritance and property hiding

The Java class has three access controls: private, protected, and public, and is represented as a default access control state when the three access control characters are not written. Therefore, there are four levels of access control altogether.

The specific access control performance is as follows:

The property or method of private modification is unique to the class and cannot be accessed directly in any other class;

Default does not write, such modified properties or methods have the package access attribute, the other classes in the same package can be accessed;

Protected-Modified properties or methods can be accessed in other classes in the same class, and also in subclasses that are not in the same package;

Public-decorated properties or methods can be accessed directly in the outer class.

When a subclass inherits the parent class, the subclass can inherit the properties and methods (generally non-private adornments) in the parent class that have access control permissions, and the subclass does not inherit from the properties and methods that are unique to the private decorated parent class.

When a subclass needs to change the inherited method, it is often said to override the parent class. Once overridden, this method of the parent class behaves as hidden for the subclass. After the object of the subclass calls this method, it is called after the subclass is overridden

method, but there are two ways in which you can invoke the original method of the parent class in the subclass object:

  1. Force the Subclass object type to be converted to the parent class type and make the call;

2. Call through Super.

Similarly, if a property of the same name in the parent class is defined in a subclass, the parent class property is represented as hidden in the subclass.

4. This and super in succession:

The This in the constructor represents the object reference that is currently being initialized, and this in the method represents the object reference that is currently calling this method. This specific usage is shown in a few ways:

1. When there are multiple overloaded constructors, and one constructor needs to call another to construct it, it is called with this (Param) in its first row and only in the first row;

2. When a method in the object needs to call the other methods in this object, use this as the keynote, or do not write, in fact, the default is this as the keynote;

3. When the object property and the local variable name in the method are the same, in this method you need to explicitly use this as the keynote to represent the properties of the object, if this problem does not exist, you can not explicitly write this.

In fact, one of the problems involved is the search rule for variables: first local variables, and the variables defined in the current class, the variables that can be inherited by the quilt class defined in the parent class, and the parent class ...

Super represents calling the corresponding properties and methods in the parent class. In the method, if you need to call the method of the parent class, be sure to write it in the first line

General Example:

 Packagecom.wen1227; Public classNiao {PrivateString name; Private intAge ;  PublicNiao (String name) { This. name=name; }     PublicNiao (String name,intAge ) {         This. name=name;  This. age=Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. name=name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. age=Age ; }             Public voidZou () {System.out.println (name+ "Walking"); }}

Create a Niao class that defines a name and age attribute as private.

Then two constructors are defined here for overloading, one with one parameter and one with two parameters.

Because the properties defined above, name and age, are private, so when the post-face class inherits from these properties, it is necessary to define a method in the parent class to get the name and age and then define a method to change the contents of name and age.

Finally, we define a Zou method to let him output a content.

 Packagecom.wen1227; Public classXiqueextendsNiao { PublicXique (String name) {Super(name); }     PublicXique (String name,intAge ) {        Super(Name,age); }         Public voidZou () {Super.        Zou (); System.out.println ( This. GetName () + "Feifeifeifeifei" + This. Getage ()); }}

This defines a xique as a subclass that inherits the properties of the parent class Niao, which also writes two constructors for overloading, but the subclass Xiqie contains no name and age content, only calls the parent class, so the constructor example defined here uses the Super attribute.

Also in the subclass Xique there is a method Zou, and then write a super call the parent Class Zou property (from the results you will be at a glance), but the Zou properties have been modified, which is called rewriting.

 Package com.wen1227;  Public class Test {    publicstaticvoid  main (string[] args) {        =New Xique ("Zhang San", 3);        X.zou ();         =new niao ("Lisi");        N.zou ();    }}

Finally take a main function call, first new a xique, let it xique Zou, then new a niao, let it niao Zou, the result of the final output is

Zhang San is walking Zhang San Feifeifeifeifei3lisi is walking.

Because the first call to Xique, he first called the super. Zou (); So output the parent class first, then execute the xique System.out.println (this.getname () + "Feifeifeifeifei" +this.getage ()), and finally execute the Zou of the parent class. So it's three lines, at a glance.

5. Inheritance and combination:

From the simple realization effect, the inheritance and the combination can achieve the same goal. and are an effective way to implement code reuse.

But in the general concept level, the two have the obvious difference.

Inheritance manifests itself as a general--special relationship, a subclass is a special parent class and a is-a relationship. The parent class has a generic attribute for all subclasses.

The combination behaves as a whole--part relationship. In the composition, the "part" is extracted separately to form its own class definition, and in the "whole"

In this class definition, a section is defined as one of these properties, and through the Get and set methods, you can invoke the properties and methods in the "partial" class.

Java,extends, inheritance

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.