Jtest logo




Contents  Previous  Next  Index

CODSTA.NCAC


Never call an "abstract" method from a constructor in an "abstract" class

Description

This rule flags code that calls an "abstract" method from a constructor in an "abstract" class.

Calling abstract methods from an "abstract" class's constructor causes the object's methods to be used before it is finished using its constructors.

Example

 package CODSTA;
 
 abstract class NCAC {
     public NCAC () {
         System.out.println("Constructor: ");
         test (); // invoke abstract method from the constructor.
     }
     abstract public void test ();    
 }
 class MyClass extends NCAC {
     public MyClass (int size) {
         super ();
         System.out.println("setting size to : " +size);
         _size = size;
     }
     public void test () {
         _size++;
         System.out.println("Increament : " +_size);
     }
     private int _size = 0;
 }
 
 Result of the above code is:
  MyClass mc = new MyClass (50);
  Constructor:
  Increment : 1        // super class's constructor called test();
  setting size to : 50  // finish executing MyClass' constructor.

Reference

Warren, Nigel, and Bishop, Philip. Java in Practice. Addison-Wesley, 1999, pp.103-104.


Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft