Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: Constructors - super example

JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz


Bookmark and Share

Example of class without parameterless constructor

/////////////////// class without a parameterless constructor.
//  If any constructor is defined, the compiler doesn't
//  automatically create a default parameterless constructor.
class Parent {
    int _x;
    Parent(int x) {   // constructor
        _x = x;
    }
}

////////////////// class that must call super in constructor
class Child extends Parent {
    int _y;
    Child(int y) {  // WRONG, needs explicit call to super.
        super(0);
        _y = y;    
    }
}
In the example above, there is no explicit call to a constructor in the first line of constructor, so the compiler will insert a call to the parameterless constructor of the parent, but there is no parameterless parent constructor! Therefore this produces a compilation error. The problem can be fixed by changing the Child class.
////////////////// class that must call super in constructor
class Child extends Parent {
    int _y;
    Child(int y) {  // WRONG, needs explicit call to super.
        _y = y;    
    }
}
Or the Parent class can define a parameterless constructor.
/////////////////// class without a parameterless constructor.
//  If any constructor is defined, the compiler doesn't
//  automatically create a default parameterless constructor.
class Parent {
    int _x;
    Parent(int x) {   // constructor with parameter
        _x = x;
    }
    
    Parent() {        // constructor without parameters
        _x = 0;
    }
}
A better way to define the parameterless constructor is to call the parameterized constructor so that any changes that are made only have to be made in one constructor.
    Parent() {        // constructor without parameters
        this(0);
    }
}
Note that each of these constructors implicitly calls the parameterless constructor for its parent class, etc, until the Object class is finally reached.
 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy