Object oriented programming in dart concepts
No introduction, let us go straight to concepts :-)
What is object oriented programming?
It is a paradigm that involves classes and objects. First, we will understand paradigms in detail then we will understand how object oriented programming falls in this framework. Then we will explore what are classes and objects. After that we understand object oriented programming in Dart language. So, keep scrolling to get crystal clear concepts.
What is a paradigm?
Paradigm is a method to solve a problem. It may include some tools or coding languages. We can use any programming language to solve a problem but that approach used to solve it is called a paradigm.
Types of paradigms
Paradigms separate programming languages into different categories on the basis of features of various programming languages.
1. Imperative programming paradigm
Statements change the state of the program. They give commands to the computer. It means this type of programming tells the computer how it should accomplish its target.
Statements are parts of syntax of programming languages. It tells the computer to perform some action. When we code, we write a bunch of statements which are a sequence of commands that the computer should carry out.
State means the memory of the system which stores the previous commands and user interactions. A system that remembers this stuff is called stateful.
Control flow means the order in which statements or instructions of a programming language should be carried out. It is expressed in imperative programming.
2. Declarative programming paradigm
This type of programming does not specify to the program how it should accomplish what it is supposed to accomplish.
It means this type of programming expresses the computation without expressing the control flow.
Computation is calculation which may or may not include arithmetic.
Object oriented programming is a type of Imperative programming paradigm.
The basis of object oriented programming is units called objects.
Objects may be variables, data structures or data structures. They are reused time and again.
What are classes and objects?
Classes give the blueprints for defining objects.
Let us take an example:
We are trying to describe a person. The person becomes the object. The person_name becomes the class. Different persons will have different person_name values. Again, height becomes a class. So, there can be a bunch of different classes to build the same object, person.
This is the syntax of a Dart class.
Class in dart
class ClassName {
<field name>
<getter/setter>
<constructor>
<functions>
}
Below is an example of the syntax.
class Bird {
String color = "purple";
void disp() {
print(color);
}
}
Let us understand this syntax.
There is the class word written before Bird. It symbolizes that Bird is the name of the class.
In the above example, we have considered only the field and a function. In the coming examples, we will also explore about the getter/setter and constructor.
Here, the field is color. It is a string variable. However, we could use any type of variable here. This field stores data about the object.
We have also taken the function, void disp() inside which we have print. That means it is an action that the computer has to take. It will display the color of the bird which is purple as the output.
To use getter and setter in Dart, let us understand the concepts involved first. Then we go to examples.
Why do we have to use getter/setter? Even if we do not code for getter/setter, there are default getter/setter always. But, we use getter/setter to override the default values. Getters and setters are also called mutators or accessors.
We have seen what fields in class are. We use getter/setter to change the data of the class field. Getter reads the data and saves it as a variable. While setter changes the data which getter reads and sets it to a certain value.
Let us use the getter/setter method in the above program.
First, we create the class, Bird.
class Bird {
String color;
String get getName {
return color ;
}
set setName( String name ) {
color = rang;
}
}
What have we done? We have used the getter and the setter.
Now, we will create an instance of this class. An instance of a class has particular values for the class. Here, there is a field called color. When we assign the value purple to it then it becomes the instance of the class.
void main () {
Bird panchi = Bird();
panchi.setName = "prettybirds";
print ("I love ${panchi.getName}");
}
What have we done?
First, we set a class called Bird.
Then, we gave it a field. This field was a string variable which we named Bird.
Till now, we have not assigned any value to Bird which means we do not yet know what color the bird is. But that does not matter.
Syntax: Defining a getter
Return_type get identifier
{
// statements
}
What does String get getName {} mean?
We write String because color is a string variable which is also the return type. We use get to define the getter. Whatever follows get is the identifier or the name you supply to your variable. In this case, getName() returns a string value.
Similarly, we have set and setName.
Syntax: Defining a setter
set identifier
{
// statements
}
Four basic concepts of Object Oriented Programming (OOP)
1. Encapsulation
It refers to creating a protective barrier around the class so that it remains unaffected from the rest of the code.
For example, one class may be passport-details which should be protected from other objects. Hence, this class should be made private. We do this my encapsulation.
2. Abstraction
Consider your calculator. When you are typing the input and hitting the "=" key then you don't have to think about how the calculator is working from the inside. You are only pressing the buttons from the outside. That means the calculator is a black box and the buttons are the commands. You can get the benefits of using a calculator without knowing how it does the work for you. That is the essence of abstraction.
In coding, suppose we have a class representing dog. Now there can be functions like dogbark() or dogwalk(). Of course there is code inside this function as well but when the end user implements these functions, they do not know about the codes inside the classes. They only implement the function and get the dog barking or walking.
3. Inheritance
It means organizing the classes into hierarchies. There are parent and child class inside the classes.
When one class inherits from another class (a.k.a. parent class) then they get the functionalities of the parent class. Then more data can be stored on it and the functionalities can be extended. This allows the reuse of code inside the parent class.
The parent class is also called the superclass and the child class is also called subclass.
Consider this analogy. lizards, birds, and humans all have five digits (fingers) in each limb. That is the parent class or superclass. But, when the digits of the bird are improved in functionality for flying and that of the human for grasping then the human forelimb or the bird forelimb is the subclass or the child class.
4. Polymorphism
Polymorphism means when something occurs in many different forms. You can access objects of various kinds using the same interface. You can implement the different types of objects differently.
Let us take an example. Suppose we have a parent class vertebrate. Now the child classes are bird and human. There is a function usedigits in the parent class vertebrate. Now, the child classes will have fly and hold functions. You will be able to invoke usedigits in both bird and human and in bird, it will fly whereas, in human it will hold.
Comments
Post a Comment