More on Constructors


You call a superclass constructor from a subclass constructor by using the super keyword, as demonstrated earlier. A call to a superclass constructor must appear as the first line in a subclass constructor.

A subclass extends, or builds upon, a superclass. You can think of a subclass object as an outer layer, or shell, around an object of the base class. Before the subclass object can exist, Java must build and properly initialize an object of the superclass. The following language test demonstrates how classes are constructed, using a demo class called SuperClass and its subclass SubClass.

 // SuperClassTest.java import junit.framework.TestCase; public class SuperClassTest extends TestCase {    public void testConstructorCalls() {       SuperClass superClass = new SubClass();       assertTrue(SuperClass.constructorWasCalled);    } } // SuperClass.java class SuperClass {    static boolean constructorWasCalled = false;    SuperClass() {       constructorWasCalled = true;    } } // SubClass.java class SubClass extends SuperClass {    SubClass() {    } } 

The Java virtual machine requires every class to have at least one constructor. However, you can code a class without explicitly defining a constructor. If you don't define a constructor Java will automatically generate a default, no-argument constructor.

If you do not provide a super call in a subclass constructor, it is as if Java inserted a call to the no-argument superclass constructor. Even constructors that take parameters call the no-argument superclass constructor by default.

 // SuperClassTest.java import junit.framework.TestCase; public class SuperClassTest extends TestCase {    public void testConstructorCalls() {       SuperClass superClass = new SubClass("parm");       assertTrue(SuperClass.constructorWasCalled);    } } // SubClass.java class SubClass extends SuperClass {    SubClass(String parm) {    } } 

The test constructs a new SubClass instance, passing in a String as a parameter. The parameter information appears in bold. The test then demonstrates that creating a SubClass instance results the execution of the SuperClass no-argument constructor.

Remember, if you do supply a constructor with arguments in a class, Java does not automatically supply a no-argument constructor. In this situation, any subclass constructors must explicitly call a superclass constructor, otherwise you receive a compilation error.

 // This code will not compile // SuperClass.java class SuperClass {    static boolean constructorWasCalled = false;    SuperClass(String parm) {       constructorWasCalled = true;    } } // SubClass.java class SubClass extends SuperClass {    SubClass(String parm) {    } } 

The above code generates the following compiler error:

 SubClass.java: cannot find symbol symbol  : constructor SuperClass() location: class studentinfo.SuperClass    SubClass(String parm) {                                        ^ 

The subclass constructor should probably be:

 class SubClass extends SuperClass {    SubClass(String parm) {       super(parm);    } } 



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net