Multilevel Inheritance

did rust-lang support multilevel inheritance?.
if not, is there any other way to achieve it.

Hi. Does this help? https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#using-supertraits-to-require-one-traits-functionality-within-another-trait

1 Like

Rust doesn’t support inheritance. So whatever you mean by “multilevel inheritance” (inheritance from a thing that inherits from another thing? I’ve never heard this term before), Rust probably doesn’t have that.

As scottmcm mentioned in your other thread, Rust accomplishes class-like behavior in different ways. This includes “inheritance”. There is no one best way to “do inheritance in Rust” because inheritance is used for different things, but Rust generally has the tools you need to accomplish anything you want. It’s just not always the same tools from one thing to the next.

Unlearning “classical OOP” and adjusting to what one does in Rust (“trait-based OOP”? Is there a term for this?) may well be the most difficult thing about the language, for someone who’s already been taught the “classical” way. If you just search the archives for OOP buzzwords you will find many threads from people struggling with this.

If you have a concrete, real-world-code example of a case where you think inheritance is needed, please share it so we can make suggestions for corroding it. :wink:

7 Likes

https://www.programiz.com/cpp-programming/multilevel-multiple-inheritance

This is at least one resource that uses that term. And what is accomplished in here can be accomplished using the reference in my other post.

so rust is a Procedural oriented oriented programming language that support Abstraction and polymerism

It also supports a limited form of functional programming.

I don’t know if I’d call Rust procedural. You can write procedural programs in it, sure, but it has all the capabilities of an “object oriented” language too, and they are used in the standard library as well as in real programs.

If there is an “oriented” term for Rust as a whole, I’d be inclined to call it data oriented, but that seems simplistic. Rust is a language that supports multiple paradigms. You can write object-oriented programs in Rust. It might not be quite as convenient as in Java, but it works fine. Maybe the problem is in trying to apply terms like “procedural” and “object-oriented” to a grab bag of language features rather than to the design of a specific program. Surely a procedural program doesn’t become “object-oriented” because it’s written in Java.

3 Likes

I have to agree with @trentj, the orientedness of a language depends on how you write the program, if I use a bunch of trait objects and various levels of Trait: Inheritance, then I can say I have an object-oriented program. If I mainly just use function calls, and very basic data management/structures then I could call it procedural. If I use alot of f: &impl Fn(Args) -> Ret then I could say that there’s a bit of a functional aspect (I’ve never used/touched a purely functional language, so please correct me if necessary). I could even claim that it’s a trait oriented language, if my trait uses are mostly “shallow” in that they mostly don’t inherit and instead rely on associated types, and generics, etc.

For example, my dynamic storage library is:

  • 85% Trait-oriented
  • 10% Object oriented
  • 5% Functional (?)

Rust is basically a group of all of them, with a strong bias towards trait-oriented.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.