Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java Notes

Constructors - super

Every object contains the instance variables of its class. What isn't so obvious is that every object also has all the instance variables of all super classes (parent class, grandparent class, etc). These super class variables must be initialized before the class's instances variables.

Automatic insertion of super class constructor call

When an object is created, it's necessary to call the constructors of all super classes to initialize their fields. Java does this automatically at the beginning if you don't.

For example, the first Point constructor (see Constructors) could be be written

public Point(int xx, int yy) {
    super();  // Automatically inserted
    x = xx;
    y = yy;
}

Explicit call to superclass constructor

Normally, you don't explicitly write the constructor for your parent class, but there are two cases where this is necessary:

  • Passing parameters. You want to call a parent constructor which has parameters (the default construct has no parameters). For example, if you are defining a subclass of JFrame you might do the following.
    class MyWindow extends JFrame {
        . . .
        //======== constructor
        public MyWindow(String title) {
            super(title);
            . . .
    
    In the above example you wanted to make use of the JFrame constructor that takes a title as a parameter. It would have been simple to let the default constructor be called and use a setter method as an alternative.
    class MyWindow extends JFrame {
        . . .
        //======== constructor
        public MyWindow(String title) {
            // Default superclass constructor call automatically inserted.
            setTitle(title);  // Calls method in superclass.
            . . .
    
  • No parameterless constructor. There is no parent constructor with no parameters. Sometimes is doesn't make sense to create an object without supplying parameters. For example, should there really be a Point constructor with no parameters? Altho the previous example (see Constructors) did define a parameterless constructor to illustrate use of this, it probably isn't a good idea for points.

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.