Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Object-oriented language basics, Part 4

Inheritance: Build objects in layers

  • Print
  • Feedback

Page 3 of 10

There are two more things to keep in mind when calling a superclass constructor. First, you can only call a superclass constructor from a subclass constructor. You cannot make that call from any other subclass method. Second, never place any subclass constructor code ahead of its superclass constructor call. For example, changing the order of statements in the previous code fragment's Circle constructor to this.radius = radius; super (x, y); results in a compiler error. Why does that rule exist? A subclass constructor's initialization code might depend on the values of fields declared in a superclass layer. If the superclass's fields have not initialized, the subclass constructor would not properly initialize. By forcing you to specify a superclass constructor call before any other initialization code, the compiler prevents such logic errors from occurring.

  • Print
  • Feedback

Resources