Easy to Learn Java: Programming Articles, Examples and Tips

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


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
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

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

Bookmark and Share Every object contains the instance variables of its class. What isn't so obvious is that every object also has all the fields of all super classes (parent class, grandparent class, etc).

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) {
            . . .
    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.

 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