Refactoring 022 – Extract Common Ancestor

Extract a common abstract class to mimic real-world structure.


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri

Make your class hierarchy clear and flexible

TL;DR: Extract a common abstract class to mimic real-world structure.

Problems Addressed

Related Code Smells

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xiv?embedable=true

https://hackernoon.com/code-smell-255-addressing-parallel-hierarchies-in-code?embedable=true

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xiii?embedable=true

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-ix-7rr33ol?embedable=true

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxiii?embedable=true

Steps

  1. Identify common behaviors in both classes.

    \

  2. Create an abstract class with shared behavior and no implementation.

    \

  3. Move common logic to the abstract class.

    \

  4. Update subclasses to inherit from the abstract class.

Sample Code

Before

class Car {
    void drive() {
        System.out.println("Driving a car");
    }
}

class Truck extends Car {
    void load() {
        System.out.println("Loading cargo");
    }

    void unload() {
        System.out.println("Unloading cargo");
    }
}

// Truck reuses driving method
// Overriding it would be another code smell
// Violating Liskov Substitution rule

After

abstract class Vehicle {
    // 2. Create an abstract class
    // with shared behavior and no implementation
    abstract void drive();
    // 1. Identify common behaviors in both classes
    // 3. Move common logic to the abstract class
}

class Car extends Vehicle {
    // 4. Update subclasses to inherit from the abstract class
    void drive() {
        System.out.println("Driving a car");
    }
}

class Truck extends Vehicle {
    // 4. Update subclasses to inherit from the abstract class
    void drive() {
        System.out.println("Driving a truck");
        // Implementation is different than the car's
    }

    void load() {
        System.out.println("Loading cargo");
    }

    void unload() {
        System.out.println("Unloading cargo");
    }
}

Type

  • [x] Semi-Automatic

Safety

This refactoring is safe if you identify all common behaviors correctly and move one method at a time running the tests.

Why is the Code Better?

It reduces duplication, simplifies maintenance, and makes it easier to extend functionality by adding new concrete realizations.

How Does it Improve the Bijection?

By introducing an abstract class, the code better reflects the real-world hierarchy, creating a clear relationship between the generic and specific types.

Refactor with AI

| Without Proper Instructions | With Specific Instructions | |----|----| | ChatGPT | ChatGPT | | Claude | Claude | | Perplexity | Perplexity | | Copilot | Copilot | | Gemini | Gemini | | Meta AI | Meta AI |

Tags

  • Inheritance

Related Refactorings

https://hackernoon.com/refactoring-013-eliminating-repeated-code-with-dry-principles?embedable=true

See also

https://refactoring.guru/es/extract-superclass?embedable=true

Credits

Image by Pexels on Pixabay


This article is part of the Refactoring Series.

https://maximilianocontieri.com/how-to-improve-your-code-with-easy-refactorings?embedable=true

\


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2025-01-23T21:22:07+00:00) Refactoring 022 – Extract Common Ancestor. Retrieved from https://www.scien.cx/2025/01/23/refactoring-022-extract-common-ancestor/

MLA
" » Refactoring 022 – Extract Common Ancestor." Maximiliano Contieri | Sciencx - Thursday January 23, 2025, https://www.scien.cx/2025/01/23/refactoring-022-extract-common-ancestor/
HARVARD
Maximiliano Contieri | Sciencx Thursday January 23, 2025 » Refactoring 022 – Extract Common Ancestor., viewed ,<https://www.scien.cx/2025/01/23/refactoring-022-extract-common-ancestor/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Refactoring 022 – Extract Common Ancestor. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/23/refactoring-022-extract-common-ancestor/
CHICAGO
" » Refactoring 022 – Extract Common Ancestor." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2025/01/23/refactoring-022-extract-common-ancestor/
IEEE
" » Refactoring 022 – Extract Common Ancestor." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2025/01/23/refactoring-022-extract-common-ancestor/. [Accessed: ]
rf:citation
» Refactoring 022 – Extract Common Ancestor | Maximiliano Contieri | Sciencx | https://www.scien.cx/2025/01/23/refactoring-022-extract-common-ancestor/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.