[Notes] java implementation of the constructor

sponsored links
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.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of [Notes] java implementation of the constructor

  • and analysis of the use of DWR

    1. Download dwr.jar, to add it to WEB-INF/lib directory 2. Modify web.xml file, add the mapping DWRServlet <servlet> <servlet-name> dwr-invoker </ servlet-name> <servlet-class> org.directwebremoting.servlet.DwrServlet </ se ...

  • Hibernate access picture sample

    General web users to upload picture at treatment would normally uses two types of strategies: First, put directly into the picture in the database Blob field; II database are stored only at the picture on the path of the server information?, Pictures stor

  • Ruby Symbol

    Ruby is a completely cross-platform object-oriented scripting language, built on the Ruby-based Web framework Ruby on Rails (ROR) is the field of web development is gaining increasing acceptance. ROR substantial use of the Ruby language features, Symbol i

  • hibernate study of the fifth chapter

    1, oracle database, user is the keyword, if a user table named user will give rise to conflicts. The approach taken in the main have two kinds: ① If you can modify the table name, it is best to change the table name tuser or other name, to avoid causing t

  • hibernate Technical Study Notes (first)

    Introduction: Model does not match (impedance mismatch) java object-oriented language, object model, its key concepts are: inheritance, association, polymorphism, etc.; database is the relational model, its key concepts are: tables, primary keys, for ...

  • hibernate the third study (mapping file configuration)

    If you do not want to use the default hibernate.cfg.xml file as a configuration file, we can also specify the configuration file name: File file = new File ( "C: \ \ myhibernate.xml"); Configuration config = new Configuration (). Configure (file

  • Hibernate query: HQL and Criteria

    HQL query methods generally used in more general way through the query query. Examples are as follows: The Criteria is a more than HQL query object-oriented approach: Created as follows: Criteria crit = session.createCriteria (Object.class); crit.add ...

  • hibernate call stored procedure

    hibernate call stored procedure

  • Based on Spring's Hibernate Search full-text search function of sample

    Database: Oracle 9i JDBC Driver: OJDBC14 Development Environment: Eclipse-JEE Spring version: Spring 2.0.6 Hibernate version: Hibernate Core 3.2.5/Hibernate Annotation 3.3.0/Hibernate Validator 3.0.0/Hibernate Search 3.0.0 Beta4 / / jdbc.properties (JDBC

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries