2. C O N T E N T S
2
o Object-Oriented Programming
o Class and objects
o Constructor
o Encapsulation
o Inheritance
o Polymorphism
GDSC
o Asynchronous Programming
o Async and Await
o Future
o Exception Handling
o Try Catch
4. DART –CLASSES AND
OBJECTS
• Dart is an object-oriented programming language, so it supports the
concept of class, object …etc.
• In Dart, we can define classes and objects of our own .
• We use the class keyword to do so.
• Dart is support object-oriented programming features like classes and
interfaces.
5. 5
• Class is the blueprint of objects and class is the collection of data members and data function means
which include these fields, getter and setter, and constructor and functions.
• Objects are the instance of the class and they are declared by using new keyword followed by the
class name .
Declaring class in Dart : Declaring object in Dart:
Syntax: Syntax:
class class_name {
// body of class
}
Var object_name = new class_name(
[arguments] );
• Class is the keyword use to initialize the
class.
• Class_name is the name of the class.
• Body of class consists of fields,
constructors, getter and setter methods,
etc.
• new is the keyword use to declare the instance of the class
• Object_name is the name of the object its naming is similar to
the variable name in dart
• class_name is the name of the class whose instance variable is
been created.
• Arguments are the input which are needed to be pass if we
are willing to call a constructor.
6. 6
• After the object is created to access the fields we
use ( . ) operator for that purpose.
Output :
Welcome to Addis Ababa
7. CONSTRUCTOR IN
DART
• A constructor is a special method used to initialize an object
• It is called automatically when an object is created and it can be used to set the initial
values for the object’s properties
8. 8
• The constructor’s name should be the same as the class name
• Constructor doesn’t have any return type.
• If you don’t define a constructor for class, then you need to set the value of the properties manually
Without Constructor
Person person = Person();
person.name = “John”;
Person.age = 30;
WithConstructor
Person person = Person( “ John “ , 30 ) ;
Syntac for declaration Constructor
Class ClassName {
// Constructor declaration : same as class name
ClassName() {
//body of the constructor
}
}
10. ENCAPSULATION IN
DART
• Encapsulation is one of the important concepts of oop.
• Encapsulation means hiding data within a library, preventing it from outside
factors.
• It helps you control your program and prevent it from becoming too
complicated.
11. 11
• To Achieve Encapsulation we need to Declare the class properties as private by using underscore ( _ ) .
• Providing public getter and setter methods to access and update the value of private property.
• Getter and setter methods are used to access and update the value of private property.
• Getter methods are used to access the value of private property .
• Setter methods are used to update the value of private property
13. C O N C E P T O F I N H E R I TA N C E
In Dart one class can inherit another class i.e dart can create a new class from an
existing class.
We make use of extend keyword to inherit another class
Parent Class – it is the class whose properties are inherited by the child class.
Child Class – it is the class that inherits the properties of the other classes.
14. Output :
This is This is class A
• Child classes inherit all properties and methods except
constructors of the patent class.
• Like Java, Dart also doesn’t support multiple inheritance
15. 15
What is Super in Dart
• Super is used to refer to the parent class.
• It is used to call the parent class’s properties
and methods.
MacBookPro display
MacBook display
Laptop display
Output :
16. POLYMORPHISM IN
DART
• Poly means many and morph means forms.
• Polymorphism is the ability of an object to take on many forms.
• As humans, we have the ability to take on many forms. We can be a
student, a teacher, a parent, a friend, and so on. Similarly, in object-oriented
programming, polymorphism is the ability of an object to take on many
forms.
18. 18
Task
1. Write a dart program to create a class Laptop with properties [ id , name , ram ] and
create 3 objects of it and print all details.
2. Write a dart program to create a class Animal with properties [ id, name , color ].
Create another class called Cat and extends it from Animal. Add new properties
sound in String . Create an object of a Cat and print all details.
20. ASYNCHRONOUS
PROGRAMMI NG I N
DART
• Asynchronous Programming is a way of writing code that allows a program to do
multiple task at the same time.
• Time consuming operations like fetching data from the internet, writing to a database,
reading from a file, and downloading a file can be performed without blocking the
main thread fo execution
21. 21
Synchronous Programming
• In Synchronous programming the program is executed line by line, one at a time.
• Synchronous operation means a task that needs to be solved before proceeding to the next one
void main() {
print("First Operation");
print("Second Operation");
print("Third Operation");
print("Last Operation");
}
• Let’s assume Second Operation takes 5 seconds to load
then Third Operation and Last Operation need to wait
for 3 seconds. To solve this issue you can use
asynchronous programming.
22. 22
Asynchronous Programming
• In Asynchronous programming the program execution continues to the next line without waiting to
complete other work.
• It simply means, Don’t Wait
• It represents the task that doesn’t need to solve before proceeding to the next one.
void main() {
print("First Operation");
Future.delayed(Duration(seconds:5),()=>print('Second Operation'));
print("Third Operation");
print("Last Operation");
}
First Operation
Third Operation
Last Operation
Second Operation
Output
23. W H Y W E N E E D A S Y N C H R O N O U S ?
23
• To Fetch Data From internet,
• To write something to database,
• To execute a long-time consuming task,
• To read data from file,
• To download file etc.
24. FUTURE IN DART
• In dart, the Future represents a value or error that is not yet available,
• It is used to represent a potential value, or error, that will be available at soe time in
the future
• You can create a future in dart by using Future class.
24
25. 25
How to use Future in dart
you can use future in dart by using then() method
26. A S Y N C A N D AWA I T I N D A R T
26
• Async/await is a feature in dart that allows us to write asynchronous code that looks and behaves like
synchronous code, making it easier to read
• When a function is marked async, it signifies that it will carry out some work that could take some time and
will return a future object that wraps the result of that work
• The await keyword allows you to delay the execution of an async function until awaited Future has finished.
This enables us to create code that appears to be synchronous but is actually asynchronous.
To define an Asynchronous function, add async before the function body
The await keyword work only in the async function.
28. H A N D L I N G
E R R O R S
You can handle errors in the dart async
function by using try-catch .
28
29. 29
Task
1. Write a program to print current time after 5 seconds using
Future.delayed()
2. Write a program in dart that uses Future class to perform multiple
asynchronous operations, wait for all of them to complete, and then
print the results.
30. EXCEPTION HANDLING
IN DART
• An exception is an error that occurs at runtime during program execution.
• When the exception occurs, the flow of the program is interrupted, and the
program terminates abnormally. There is a high chance of crashing or
terminating the program when an exception occurs. Therefore, to save your
program from crashing, you need to catch the exception.
30
31. T RY & C AT C H
I N D A R T
• Try – you can write the logical
code that creates exceptions in
the try block.
• Catch – when you are uncertain
about what kind of exception a
program produces, then a
catch block is used. It is written
with a try block to catch the
general exception
32. F I N A L LY I N
D A R T T RY
C AT C H
• The finally block is always
executed whether the
exceptions occur or not.
• It’s optional to include the final
block, but if it is included, it
should be after the try and
catch block is over.
33. T H R O W I N G
A N
E X C E P T I O N
• The throw keyword is used to
raise an exception explicitly
• A raised exception should be
handled to prevent the
program from exiting
unexpectedly .