Username:
Password:
Java 6 Tutorials

Introduction / Constructors

Subtitles of the Movie

Constructors; every class has at least one constructor. Some have more. Whenever an object comes about, it comes about from a class definition and one and only one constructor is called to initialize the object. Here is a simple class with two constructors. Now, when you call a constructor in the creation of an object, Java looks at the type of argument that's being passed to the constructor to determine which one to use. This first one, the constructor without any arguments is knows as the default constructor. Of course, you know all this. But there are some other things, some hidden things about constructors that you may not know and they can be important. Here's the same class with a slightly different name. Other than that, it's exactly the same. Notice the call to super that's been inserted at the top of each constructor. It's actually always been there. If you don't put in a call to super, the compiler inserts one for you and it's always the first line of code. You can't call the super constructor from anywhere else. The automatic call to super is always a call to the default constructor of the super class. If you want to call a different constructor, you will have to do that yourself. Look at this example. The sphere class extends the circle class that we looked at before. The default constructor of the sphere class only calls the default constructor the super class, the circle class to set the radius to the default of one. However, the sphere constructor that accepts the radius value as an argument uses a call to super to set the radius in the super class. Notice in this example the default constructor doesn't do anything. All it does is call super. Even though you can't see the code. Now, if you were to find a class that doesn't have a constructor, you'll get one anyway. A default constructor is automatically generated for you. You always have a constructor. On the other hand, if you define one or more constructors, and don't define a default constructor, the default constructor is not generated. That is if you define any constructor at all, you won't get the automatic generation of the default constructor. Just remember this; you always have a constructor. If you put one in, Java won't. Also, the first line of code in every constructor is a call to super. If you put one in, Java won't. Occasionally you'll see something like this; a class with a protected constructor. Sometimes you'll find a public constructor along with it and you can use that, but sometimes you won't. This means that you can only use the class as a super class and extend it because only a subclass can call a protected constructor. It often means that there's a method call somewhere that you can make to create objects as a class. It's really nothing more than a sort of sneaky way of writing an abstract class.

Tutorial Information

Course: Java 6
Author: Arthur Griffith
SKU: 33858
ISBN: 1-934743-59-3
Release Date: 2008-02-29
Duration: 7 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 81,350 Video Tutorials (20,800 free)
  • Video Available as Flash or QuickTime
  • Over 782 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available