The 7.2.1 initialization base class of Java programming idea

Source: Internet
Author: User

Inheritance is an integral part of all OOP languages. But inheritance does not just replicate the interface of the base class. When you create an object that exports a class, the object contains a child object of a base class. This sub-object is the same as the object you created directly from the base class. The difference is that the latter comes from outside, while the child objects of the base class are wrapped inside the exported class object.

The correct initialization order of the base class sub-objects can not be said to know, as if all know the same point, just to see, feel that they have mastered, in fact, not. It was just a thought. Oh, above his business. See the relevant chapters today, turn over and feel that you know it. Perhaps the same knowledge point has been so many times this thought, so today decided not only to see, not just think of know. Let's hit some examples. Did the development of 3.5, it is not a rookie of it. Before the project has been arranged quite compact, more than three years mostly just in the realization of the function, catch the progress. To study the nature with little or no knowledge. It is now three or four months in the project idle period. Once thought should return to the essence Good book and the language essence not blindly realizes realizes. Well, open a deserted and get back to the chase. Let's start by looking at the simple Java small example that I knocked on:

The correct initialization of a base class sub-object, there is only one way to guarantee this, call the base class constructor in the constructor to perform the initialization, and the base class constructor has all the knowledge capabilities required to perform the base class initialization. Java automatically inserts a call to the base class constructor in the constructor of the exported class.

Default constructor

Package com.ebao.java.constructor;

public class ClassA {
ClassA () {
System.out.println ("==============classa");
}
}

Package com.ebao.java.constructor;

public class ClassB {
ClassB () {
System.out.println ("==============CLASSB");
}
}

Package com.ebao.java.constructor;

public class ClassC extends ClassA {
ClassB cb = new ClassB ();
public static void Main (string[] arg) {
ClassC cc = new ClassC ();
}
}

Operation Result:

==============classa
==============classb

When you see an instance of a class, the build process is scaled down from the base class. Then the member object. So the base class constructor is always called and is called before the class constructor is exported.

Constructors with parameters

The above example is the default constructor, which is all constructors without parameters. The compiler can easily call them because there is no need to think about what parameters to pass on. However, if there is no default base class constructor, or if you want to invoke a base class constructor with parameters. You must explicitly write a statement that calls the base class constructor with super, with the appropriate argument list:

public class Game {
Game (int i) {
System.out.println ("---------------------Game");
}
}

Package com.ebao.java.constructor;

public class Boardgame extends Game {
boardgame (int i) {
Super (i);
System.out.println ("--------------------boardgame");
}
}

Package com.ebao.java.constructor;

public class Chess extends Boardgame {
Chess (int i) {
Super (i);
System.out.println ("--------------------Chess");
}
public static void Main (string[] arg) {
Chess cs = new Chess (7);
}
}

Operation Result:

---------------------Game
--------------------boardgame
--------------------Chess

Call the base class constructor if it is not displayed in the subclass or call the class base constructor instead of the first sentence compile error follows

The 7.2.1 initialization base class of Java programming idea

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.