13 lines
701 B
Markdown
13 lines
701 B
Markdown
|
# Inheritance, Properties and Polymorphism
|
||
|
|
||
|
Rust does not allow extending classes. Instead the base class is stored as a
|
||
|
field in the extending class. Polymorphism can then be achieved by passing the
|
||
|
field instead of the whole object to a function. Although casting back to the
|
||
|
advanced class or overriding functions is not possible.
|
||
|
|
||
|
Properties don't exist either, like in java. But replacing them with getters and
|
||
|
setters is also discouraged. Quoting from a reddit post 'Expose behavior, not
|
||
|
state'. That means you should present functions to outside viewers and not your
|
||
|
plain data. An object should be able to do all its intended functions itself
|
||
|
without having an outside observer read its data.
|