Knoldus Blog Audio
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
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.
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.
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.
It is a basic unit of Object-Oriented Programming and represents real-life entities.
Creating an object in Java.
Creating an object in Rust.
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.
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.
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 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.
Java supports the following type of inheritance:-
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.
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
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.
Data abstraction is a way by which only important data display to users.
Abstraction in Java
Abstraction in Rust using
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.