Hello, everyone.
In this video, I will talk about how
you can implement the object-oriented programming
principles in MATLAB.
The four principles of object-oriented programming
are encapsulation, inheritance, polymorphism, and abstraction.
Let's start with encapsulation.
Encapsulation, as a dictionary meaning
says, enclosing something or depicting essential features.
We implement the same approach in our programming
with the concepts of classes and objects.
The basic purpose of a class is to define an object that
encapsulates data known as the property
and operation performed on that data known as method.
This design allows the internal workings of the classes
to be hidden from code outside of the class.
To further implement the mechanism of hiding features,
we have access modifiers by which
you can restrict the access of the methods
and properties of one class from another class.
We have three types of access modifiers.
Public with unrestricted access.
Protected access from methods in a class or its subclasses.
Private access by class methods only.
Let's see how we can implement this in MATLAB.
Here we demonstrate a basic example of a class employees.
The properties defined are name and base salary.
As we have mentioned here, no access
attributes so they are public by default.
Date of birth and address are private properties accessed
only by class methods.
We can also give specific types of access
to particular classes.
Like, GetAccess to read the value
of properties or SetAccess to assign
the value of the property.
Here the value of the property state of joining and department
can be read from these three subclasses.
Then we have two methods, constructor employee
and a function getName.
Let's see how we can initiate the object
by calling the constructor.
So we name the object as obj is equal to class name employees.
Then the value to our parameters name as neha and 1,000 as base
salary.
And we have our object here.
Next, we see the principle of inheritance.
To implement this, we organize classes into hierarchies.
For example, we have a class employees
with its parameters name and base salary known as the base
class or superclass.
Engineer and Sales are subclasses
having their own properties, products, and commissions.
Inheritance expresses an Is-a or has-a relationship
between two objects.
Advantages of eliminating redundant code
are reusing the code from superclass to subclasses.
The subclasses inherit the public properties
of its superclass.
So let's implement this hierarchy in MATLAB.
We have our base class, employees.
Next, we define its subclasses, engineer and sales,
with their own properties.
Less than sign is used to represent
the subclass and superclass relationship.
We access the superclass constructor from the base class
by the center, where object E and object S
are the output of the subclass constructors.
At the red, superclass name and any arguments
required by the respective superclass.
In MATLAB, you can check the final properties of class
with the command properties and class name.
If you observe here that the subclass engineer
has all the public properties of a superclass employee.
The other important principle is polymorphism.
Polymorphism is a technique in object-oriented programming
that allows us to get rid of long lines of code
of if and else or switch case statements.
There are two types of polymorphism, static and
dynamic polymorphism.
Static is achieved by method overloading in which we
have different methods with same name
but different parameters of one same class.
And dynamic polymorphism is achieved by method overriding
in which we have methods with the same name and parameters
but in base and derived class.
That is two different classes.
Let's get back to our example to see
how we can implement method overriding in a class
hierarchy.
Here we have a function named salary, both an engineer
and sales class with the same input arguments,
but they're calculating salaries with different mathematical
operations.
So let's see how they work.
We then initiate our objects of each class,
object E and object S. We use name and base
salary from the superclass object
and provide new values for their own properties.
Next, we call our function salary first for engineer class
as E is equal to salary with our object of engineer class
and then as a number of hours.
We do the same thing for another sales class.
You call the function salary for the sales object
and 10 number of hours.
The calculated salary was different for both as
per the mathematical operation.
What to note here is while calling
the function, the objects given decides
as to which function from which class needs to be cut.
The last principle we will talk about is abstraction.
Abstraction can be thought of as a natural extension
of encapsulation.
To implement the abstraction in our example
that we are working with, we can do
by only defining a function named salary in the base class
employee.
And its derived classes will have the functions
implementation.
Applying abstraction means that each object should only
expose a high-level mechanism for using it.
In MATLAB, we can simply do it by making
the class an abstract class.
A class is an abstract class when we either
declare the attribute abstract or an abstract method
of property.
Here, you can see that the methods and properties are only
defined in an abstract class.
But the implementation will be done
in the subclasses that supports their own specific
requirements.
At the end, I will leave you with the summary of why
each principle is implemented.
To learn more in detail, please check out the resources
linked below.
Thank you.