Does Rust Support Inheritance? Yes, No, and Maybe, All in the Same File
Does Rust Support Inheritance? Yes, No, and Maybe, All in the Same File C started with plain structs: bags of fields without attached behavior. C++ added methods and class inheritance on top, letting a Derived class reuse Base fields and virtual methods while overriding only what differed. For many developers, that transition from struct to class hierarchy defines what object orientation looks…
Rust supports inheritance in a unique way that combines aspects of both yes and no, with a hint of maybe. Inheritance in Java allows classes to inherit fields and methods automatically through the extends keyword, with selective overrides possible. However, Rust does not support this type of inheritance. Instead, Rust uses traits with default methods, which resemble default methods in Java interfaces.
These traits can provide default implementations that implementors can keep or override. Additionally, Rust uses composition by embedding one struct inside another to share implementation across multiple distinct types. This means that while Rust does not have a direct equivalent of class inheritance, it still provides mechanisms for code reuse and sharing implementation across different types.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.