Java Developer Tools

Audit - Rules - Inheritance

Description
This group contains audit rules that check for problems that are either related to the type hierarchy or can only be seen by looking that the supertypes of a type.

Rules:

Details

Abstract Specialization

Summary
Abstract classes should not be subclasses of concrete classes.

Description
This audit rule finds abstract classes that are subclasses of concrete classes. An exception is made for abstract subclasses of the class java.lang.Object.

Example
The following class declaration would be flagged as a violation because the class java.util.ArrayList is not an abstract class:

    public abstract class SpecializedList extends ArrayList

while the following class declaration would not be flagged because the class java.lang.Object is treated specially:

    public abstract Person extends Object

Do Not Subclass ClassLoader

Summary
Do not extend java.lang.ClassLoader

Description
Subclasses of java.lang.ClassLoader are flagged. In instances where ClassLoader needs to be subclassed, use java.security.SecureClassLoader.

Security Implications
By subclassing SecureClassLoader instead of ClassLoader, additional Java security measures are taken to protect against class loading vulnerabilities.

Example
The following class would be flagged as a violation:

    class A extends ClassLoader {...}

Explicit Subclass of Object

Summary
The class "Object" should not be explicit specified as a superclass.

Description
Subclasses of Object should be declared implicitly by omitting the "extends" clause, not explicitly by including "extends Object" in their declaration.

Example
The following class declaration would be flagged as a violation:

    public class Employee extends Object
    {
        ...
    }

Hiding Inherited Fields

Summary
Inherited fields should not be hidden.

Description
This audit rule finds fields that hide inherited fields. That is, it finds fields that have the same name as a visible field from a superclass. Note that private fields are not visible in subclasses, so declarations of fields with the same name as a private field declared in a superclass are not flagged.

Example
Given a class declaration like the following:

    public class Person
    {
        protected String name;
        ...
    }

The field declaration in the following class declaration would be flagged as a violation:

    public class Employee extends Person
    {
        protected String name;
        ...
    }

Hiding Inherited Static Methods

Summary
Inherited static methods should not be hidden.

Description
This audit rule finds methods that hide inherited static methods. That is, it finds methods that have the same name and compatible argument types as a static method from a superclass. The problem with defining methods like this is that it is too easy to miss the fact that the methods are static and that the subclasses method therefore does not override the method from the superclass.

Example
Given a class declaration like the following:

    public class Person
    {
        public static Person newNamed(String name)
        {
            ...
        }
        ...
    }

The method declaration in the following class declaration would be flagged as a violation:

    public class Employee extends Person
    {
        public static Person newNamed(String name)
        {
            ...
        }
        ...
    }

Implicit Subclass of Object

Summary
The class "Object" should be explicit specified as a superclass.

Description
Subclasses of Object should be declared explicitly by including "extends Object" in their declaration, not implicitly by omitting the "extends" clause.

Example
The following class declaration would be flagged as a violation:

    public class Employee
    {
        ...
    }

Overriding a Non-abstract Method with an Abstract Method

Summary
An abstract method should not override a non-abstract method.

Description
This audit rule finds abstract methods that override a non-abstract method. Such a case usually represents a violation of the inherited contract.

Example
Given the following class:

    public class Widget
    {
        public int getPartCount()
        {
            return 0;
        }
    }

The method getPartCount() would be flagged as a violation in the following class:

    public abstract class CompositeWidget extends Widget
    {
        public abstract int getPartCount();
    }

Overriding Private Method

Summary
Methods cannot override a private method.

Description
This audit rule looks for methods that look like they were intended to override a method from a superclass but fail because the inherited method is defined to be a private method.

Example
Given the following class:

    public class TreeNode
    {
        private int getChildCount()
        {
            return children.size();
        }
    }

The method getChildCount() would be flagged as a violation in the following class:

    public abstract class LeafNode extends TreeNode
    {
        private int getChildCount()
        {
            return 0;
        }
    }

Prefer Interfaces to Abstract Classes

Summary
It is better to define abstract types as interfaces than classes.

Description
This rule identifies types defined by abstract classes. It flags abstract classes that define public methods that are not defined in an interface implemented by the class.

Rather than defining an abstract class to create a type, define an interface instead. In addition to the interface, include an abstract class that provides a skeletal implementation of the type, which implements the interface.

Restricted Superclasses

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.