[Notes] java implementation of the constructor

To see a java constructor on the implementation of the article, special turn follows the site:
http://blog.csdn.net/yidinghe/archive/2009/01/20/3839483.aspx

Java object is created when the will to carry out its constructor. Not only that, Java also run a parent class constructor, up 1 1 up until there is no parent. For beginners, there are three questions is not easy to get to know:

1, whether the parent class constructor will be implemented?
2, is the first implementation of the sub-class constructor is the first implementation of the parent class constructor?
3, if the parent class has multiple constructors, then Java will choose which one?

- Whether the parent class constructor will be implemented?

Yes, the parent class constructor will definitely be implemented. So, if a deep level of class, then it creates a lot of object will be to execute the constructor.

- Is the first implementation of the sub-class constructor is the first implementation of the parent class constructor?

Java would have been found up along the inheritance structure Object, and then start down the carried out sequentially from the Object constructor. The first implementation of the parent class constructor, then the child class constructor when the implementation of the parent class do not need to worry about whether members of initialization better.

- If you have more than one parent class constructor, then Java will choose which one?
If you have more than one parent class constructor, then the sub-class can choose one of the constructor (and most can only choose one) to perform. If the subclass had no choice, then Java will perform the default superclass constructor. Here is an example:

Parent class:

   1. public class SuperClass {
   2.     public SuperClass() {
   3.         System.out.println("super class constructed.");
   4.     }
   5.     public SuperClass(String name) {
   6.         System.out.println("super class constructed with name: " + name);
   7.     }
   8. }

Sub-categories:

   1. public class SubClass extends SuperClass {
   2.     public SubClass() {
   3.         System.out.println("sub class constructed.");
   4.     }
   5.     public SubClass(String name) {
   6.         super(name);
   7.         System.out.println("sub class constructed with name: " + name);
   8.     }
   9.     public static void main(String[] args) {
  10.         new SubClass();
  11.         new SubClass("world");
  12.     }
  13. }

Implementation of the results:
super class constructed.
sub class constructed
super class constructed with nameworld
sub class constructed with nameworld
Description: In the implementation of the subclass when the experienced constructor SubClass (), to go inside to find the same parent class constructor, the implementation of the parent category.

The "super (name)" this statement is removed, the implementation of the following results:
super class constructed.
sub class constructed
super class constructed.
sub class constructed with nameworld
Description: The class constructor is called, the process of the parent class constructor with a string parameter, which is public SuperClass (String name) this method. If you do not have one, it means the process of the child class constructor calls the parent class's default constructor.

Using the keyword super to call from a method within the definition of the original method. super is a placeholder class, super class, super point to the super-class rather than the current object.

分类:Java 时间:2009-11-04 人气:225
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.405 (s). 12 q(s)