Jtest logo




Contents  Previous  Next  Index

OOP.OPM


Do not `override' a private method

Description

This rules flags code where a method overrides a "private" method from a superclass.

Note that a "private" method in a super "class" is not overridden by a method with the same name in the current "class". This can be confusing. Also, if the method access in the superclass is changed to non-private, the program semantics will change because the method will then be overridden.

Example

 package OOP;
 
 class OPM extends Super {
     private void method () {}
 }
 
 class Super {
     private void method () {}
 
     public static void main (String[] args) {
         OPM x = new OPM ();
         test (x);
     }
 
     private static void (Super x) {
         x.method ();  // invokes "Super.method" not "OPM.method"
     }  
 }

Repair

Use different names for the methods to clarify that they are unrelated and to avoid possible problems if the access modifier is changed.


Contents  Previous  Next  Index

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