Skip to main content

avoid-referencing-subclasses

added in: 1.23.0
Pro+

Warns when a parent class references its child class.

Subclass references can be confusing and violate several rules of object-oriented design. Consider moving such references out of the parent class.

Example

❌ Bad:

class Parent {
// LINT: Avoid referencing subclasses in their parent classes.
void doWork(Child sub) {}

// LINT: Avoid referencing subclasses in their parent classes.
Child getChild(String value) {}

// LINT: Avoid referencing subclasses in their parent classes.
GrandChild getGrandChild(String value) {}
}

class Child extends Parent {}

class GrandChild extends Child {}

✅ Good:

class Parent {}

class Child extends Parent {}

class GrandChild extends Child {}

Additional Resources