Jtest logo




Contents  Previous  Next  Index

OPT.UNC


Avoid unnecessary casting

Description

This rule flags unnecessary casting.

All classes either directly or indirectly inherit from the class Object and any subclass is implicitly the same type as its superclass. Therefore, cast operations to superclass are not necessary.

Example

 package OPT;
 
 class UNC {
     String _id = "UNC";
 }
 class Dog extends UNC {
     void method () {
         Dog dog = new Dog ();
         UNC animal = (UNC)dog;  // unnecessary.
         Object o = (Object)dog;         // unnecessary.
     }
 }

Repair

 class Dog extends UNC {
     void method () {
         Dog dog = new Dog();
         UNC animal = dog;
         Object o = dog;
     }
 }

Reference

Warren, Nigel, and Bishop, Philip. Java in Practice. Addison-Wesley, 1999, pp.22-23.


Contents  Previous  Next  Index

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