Code Smell 161 - Abstract/Final/Undefined Classes
Tags: webdev, beginners, programming, java
Published: 2022-09-02 15:49:07

*Your classes are abstract, final, or undefined*

> TL;DR: If your language has the right tool, your classes should be either abstract or final.

# Problems

- Subclassification for [Code Reuse](https://dev.to/mcsee/code-smell-11-subclassification-for-code-reuse-1136)

- Classes with [just one](https://dev.to/mcsee/code-smell-136-classes-with-just-one-subclass-1h18) concrete subclass

- Liskov Substitution Violation

- [Yo-Yo] Problem

# Solutions

1. Declare all your leaf classes as *final* and the rest of them *abstract*.

# Context

Managing hierarchies and composition is the main task of a good software designer.

Keeping hierarchies healthy is crucial to favor cohesion and avoid [coupling](https://dev.to/mcsee/coupling-the-one-and-only-software-design-problem-2pd7).

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/580be250747d29d198fe4bbf9db41c8e)
```java
public class Vehicle
{
  // class is not a leaf. Therefore it should be abstract
    
  //an abstract method that only declares, but does not define the start 
  //functionality because each vehicle uses a different starting mechanism
  abstract void start();
}

public class Car extends Vehicle
{
  // class is leaf. Therefore it should be final
}

public class Motorcycle extends Vehicle
{
  // class is leaf. Therefore it should be final
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/712df5f99ec232c4e4d2cdaf1bdf62c0)
```java
abstract public class Vehicle
{
  // class is not a leaf. Therefore it is be abstract  
 
  //an abstract method that only declares, but does not define the start 
  //functionality because each vehicle uses a different starting mechanism
  abstract void start();
}

final public class Car extends Vehicle
{
  // class is leaf. Therefore it is final
}

final public class Motorcycle extends Vehicle
{
  // class is leaf. Therefore it is final
}
```

# Detection

[X] Automatic 

Since this is enforced by static analysis, we can't do it with most available tools.

# Tags

- Subclassification

# Conclusion

We should look back at our classes and start qualifying them either as abstract or final.

There are no valid cases for two concrete classes, one subclassifying the other.

# Relations

{% post https://dev.to/mcsee/code-smell-11-subclassification-for-code-reuse-1136 %}

{% post https://dev.to/mcsee/code-smell-136-classes-with-just-one-subclass-1h18 %}

{% post https://dev.to/mcsee/code-smell-37-protected-attributes-4n4 %}

{% post https://dev.to/mcsee/code-smell-58-yo-yo-problem-ej9 %}

# More Info

{% post https://dev.to/mcsee/coupling-the-one-and-only-software-design-problem-2pd7 %}

[Deep Subclasses](http://www.laputan.org/drc.html)

# Disclaimer

Code Smells are just my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

# Credits

Photo by [William Bossen](https://unsplash.com/@william_bossen) on [Unsplash](https://unsplash.com/s/photos/the-end)
  
* * *

> When the final design seems too simple for the amount of work you've put in, then you know you're done.

_Brady Clark_
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}