7

Object Oriented Programming Concepts in Rust

 3 years ago
source link: https://blog.knoldus.com/object-oriented-programming-concepts-in-rust/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Knoldus Blog Audio
Reading Time: 4 minutes

Object Oriented Programming (OOPs) Concept

Object Orientated programming is a methodology or paradigm to design a program using classes and objects. OOPs provides the following concepts: –

  • Class and Objects
  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction

Java OOPs Concepts

There is a big debate about how many of these concepts a language needs to implement to be considered object-oriented.

Today we gonna see how Rust implements these concepts to be an object-oriented language. In this blog, we are comparing Java And Rust’s way of implementing these concepts.

Class

The class can be defined as a collection of data members and member functions. It is a logical entity. It represents the set of properties or methods that are common to all instances of the class.

Defining a Class in Java.

m-4XeORf_ghmweBVgrpNfbQnxdHyxM-XwaZgPCZe8olt0MYJNyIMqzc2ymrTEbF5EVIxobY9qk3BDkYDISwwicqH5xg5WjdVWzDI9MrvuazgVsyzppXh54qTA1PwDGcSF-WNUhdb

To define a class in java we use the keyword class. Whereas in Rust, there is no keyword like class. In Rust, we have a struct  that helps us to achieve the same functionality. 

Defining a Struct in Rust.

-9bNOQzO9mbWp1fuiAODtQ55sJjmhY5LrcsVXDB362WHq38s9-hzOs8SgmCsD4Q-jw8JINnJ-vDFkE42Z6vVkjw3bTwKQ2cx8i3Amy8KRUmabAMSUKL7WjgoimCwpmws3E-CmwX-

Object

It is a basic unit of Object-Oriented Programming and represents real-life entities.

Creating an object in Java.

313pM1XtUJg8A1Vy85TyI1gl_OfJnFos9raUzVie7hC2WWRTlcqaBZtOGvWRMmcXrP8rE-8zxOkMcD0DOOmS8gvExZLuHyAlN_Ka39aNKS2CWVnooYx2Kwr38dnArv5gWPgPdddG

Creating an object in Rust.

l7cGwLE3kLru-Sbtm9bWuFq8k4BJuFD2P0JbU-9M2x0ejci7LWFBTxKDaxTPop7LlDjc1hcZBw3Y21OHXNxZ49EGC5qaojnCllePu1tr0iu5oDmHQofZyHb1-gz16QmtJukqT9aG

Encapsulation

Encapsulation is defined as the wrapping up of data under a single unit. This means that the implementation details of an object aren’t accessible to code using that object. Code using the instance shouldn’t be able to reach into the object’s internals and change data or behavior directly. So its acts as a shield. As a result, no one from outside can change its data.

Encapsulation in Java.

0C1aHQy4g9abGtwEcwY9OInbWm7Yt5--tRRXjlmbv1-C2IIUOVId5yG2Kbv3DnzCh6uiVA95Qkkj0MAC1cRjAW8ZXFC1RR98sFbaG30y-oy8u_JWpjv55_pEcvJb2OzsmZN_sNpe

In this code, the variables var1 & var2 are private members of the class. Therefore these variables are not accessible to any other piece of code. This way java achieves encapsulation.

Encapsulation in Rust.

nvIHO60h-HGDxautShrqV1-aVYAKEFehZ8Tox5qLqoHBkZ8snQbL8j5UlWNl382n-TSM2olP82SijN0_Q6TmPvCEbT6Z7Z-A0qCYplXNuBr9gjjUhw-fm4_4LWl8j87vysefMWEx

By declaring struct List pub. This struct now accessible to everywhere in code but its field is private. So its fields cant be modified by the external piece of code. So this is how encapsulation is achieved in rust.

Inheritance

Inheritance is a mechanism where one class is allowed to inherit the features(fields and methods) of another class. 

Implementing Inheritance in Java.

The keyword used for inheritance in java is extends.

Htd2JNvynb_6ZWQOMuI5D3EDcJfXIYpaSIq3Gs81gbt8GxtM6gDIYU8zPwSnh2t0kENIMNIoPCrIUx5-KcBLnYpgH2Mu0qRTbiF_Mo0tZInva5XRHBU9zNaoDkQTJ3SHEK4xvWEz

Java supports the following type of inheritance:-

  • Single
  • Multilevel
  • Hierarchical

K7-RtH799SqJ5yXK7u5uOsAEV0f2Z-HMMDCz7NrmwVkA4ZYGw8Bgeg85TvXH6Sf-NAFQmN9GeNwNrHgN-LrQtySFKgCv3iiFOYIDEcVYX886_A3gMCBp5uGKLRV7V5q2m8efnjM1

Implementing Inheritance in Rust.

If inheritance only means one struct inherits another struct. Then there is no way to define a struct that inherits the parent struct’s fields and method implementations. However, if you using Inheritance for code reusability. Then we can use trait method implementations instead.

IMoso9gETNSkhlK8DRfL5h_HDT9UYPeH-I-Mc2Sb6lSIagAoSaPhBnO45I9D-5MIcwquUqdrZgyubUO2ZXSoUSs-CNyMdBldvgpAKWnu7X9sq-Gy8QfBmtRG0wRLIDvWQwhyMXKZ

Polymorphism

The word polymorphism means “many forms”. The ability of a programming language to present the same interface for several different underlying data types.

Polymorphism in Java.

Java supports two types of polymorphism which are: – 

  • Compile-time / Static Polymorphism
  • Runtime / Dynamic Polymorphism

TS1SlTDQ1Vy7PAZkqieP4MYUQLr0yoEF8ojj8MIhGhZwKIGXnHB0vif9VLnlRKsigwtb8bOKZfKWvS7mgwAnLCK2WAfketc7BgT1SPZNGsKTVRZLvtalg2KEFzpaTYEDkWYSi2SW

g_uDgj_Re_wbNyxYtpZNV8vxOMW4rPubFJZjjB6_F0A0uUMz9Wa0ZjsN8ykK-QbvIJArwSjURg5om_buRLS1crv-gwZ_lBkvF-XmHt1UYC0F1Sjc_JEmvICF_3TJooMZAf_H3w8c

Polymorphism in Rust.

Rust supports two types of polymorphism which are: – 

  • Ad-Hoc polymorphism
  • Parametric polymorphism

Traits are used to create ad-hoc polymorphism. Parametric polymorphism can be achieve using generics. In parametric polymorphism, a single function can be called using different data types.

0Lx2ajxDRZdWw6d2puFyY0Hfg-_SnUiZVjQQ-rFpSHOzH9lU_WCoKrnw98QQnMlp2k_k8WuYzeU-BwdmwlJPvQdGGmFIsh8qvgBi1pbvkNaCZhUQqhsqb6R_TZIWv_eUMcONc0Lx

BVUnwLLpVcwmJ1lwTj9l0eJNe_O_A0ZEMC0cVMYLUwwxKw0FgX_COmYWO0YnsvQ3YGtCWQziS_MUMKZVBAdGtsTB_kM7ycpw45I6Wa-1_4ZJih5jgBYWRB-PkvFR6NX3c4BF_yYN

Abstraction

Data abstraction is a way by which only important data display to users.

Abstraction in Java

kQlO5hWcMHxyssz0-qAkhyDcDfjzvACxN07MZ59gXJ6LN2gkzGrc8cLlLACKhToazSWiisRN3hjrkgEB81Wub8_kaORueq3CinivEUZPIN-5wwyt-LNaHQcNLKNF6VFTn-ipVEve

6pkRd_AuFtnoggh0z4IkQWQvaJiTHVq06E3iZt9oqLF2uxeNqwAPjbHT6-FE_zuGV64sPjJ19E7qgJ1WDRvlAgjwVpDGh35O0OBdQ8W7erDIVeEP6JaCsxVTVJDYAiejsES5qYQS

Abstraction in Rust using trait

7Bd-yB8KHCQzXtK57CR3P9X0haqyhZilSJWBaoaYFso9Gm6JgCEBWvqLMc-ehX-dS_Kk8jZYdt0csXwZjsSUUA7cX17BCh2KZlWdayp9ZJSQS0cXZi0s316t-ARF9UwwYE4PXOMe

If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter: https://bit.ly/2Vdlld7.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK