0% found this document useful (0 votes)
24 views

Mobile Computing Lab2

The document discusses object-oriented programming concepts in Dart including classes, objects, constructors, encapsulation, inheritance, polymorphism, and abstract classes. It provides examples and explanations of each concept.

Uploaded by

Mai gamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Mobile Computing Lab2

The document discusses object-oriented programming concepts in Dart including classes, objects, constructors, encapsulation, inheritance, polymorphism, and abstract classes. It provides examples and explanations of each concept.

Uploaded by

Mai gamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Mobile computing lab2

Eng : hamada mostafa Eng : fatma mohammed


Lab2 content

u OOP in Dart

u Class and Objects in Dart

u Constructor in Dart

u Encapsulation in Dart

u Inheritance in Dart

u Polymorphism in Dart

u Abstract Class in Dart


Object-oriented programming
u a programming method that uses objects and their interactions to design
and program applications. It is one of the most popular programming
paradigms and is used in many programming languages, such as Dart,
Java, C++, Python, etc.
u Advantages
1. It is easy to understand and use.
2. It increases reusability and decreases complexity.
3. The productivity of programmers increases.
4. It makes the code easier to maintain, modify and debug.
5. It promotes teamwork and collaboration.
6. It reduces the repetition of code
Class and Objects in Dart
u A class is a blueprint for creating objects. A class defines the properties and methods
that an object will have.
u An object is an instance of a class You can create multiple objects of the same class.
u Output : lion 4 10
u

u
u Output : Area of rectangle is 50.
Constructor in Dart
u The constructor’s name should be the same as the class name.
u Constructor doesn’t have any return type.
u When you create a object of a class, the constructor is called automatically.
u It is used to initialize the values when an object is created.
u Output : Constructor called Name: John Age: 20 Roll Number: 1
u Output : Name: John
Phone1: 1234567890
Phone2: null
Subject: Maths
Constructor With Optional Parameters
u Output : Name: John
Age: 30
Subject: N/A
Salary: 0
Encapsulation In Dart
u hiding data within a library, preventing it from outside factors. It helps you control
your program and prevent it from becoming too complicated.
u Declaring the class properties as private by using underscore(_).
u Output : Jack
u final keyword Read-only Properties
u Properties declared with the final keyword must be initialized at the time
of declaration. You can also initialize them in the constructor .
u Output : ABC School
u Why Encapsulation Is Important?

• Data Hiding: Encapsulation hides the data from the outside world. It
prevents the data from being accessed by the code outside the class.
This is known as data hiding.

• Testability: Encapsulation allows you to test the class in isolation. It


will enable you to test the class without testing the code outside the
class.

• Flexibility: Encapsulation allows you to change the implementation of


the class without affecting the code outside the class.

• Security: Encapsulation allows you to restrict access to the class


members. It will enable you to limit access to the class members
from the code outside the library.
Inheritance In Dart
u Inheritance is a sharing of behaviour between two classes. It allows you

to define a class that extends the functionality of another class.

u The extend keyword is used for inheriting from parent class.

u Whenever you use inheritance, it always create a is-a relation between

the parent and child class like Student is a Person.

u Dart supports single inheritance, which means that a class can only

inherit from a single class. Dart does not support multiple inheritance

which means that a class cannot inherit from multiple classes.

u It promotes reusability of the code and reduces redundant code.

u It helps to design a program in a better way.

u It makes code simpler, cleaner and saves time and money on


Output : Name: John
Age: 20
School Name: ABC School
School Address: New York
Types Of Inheritance In Dart
1. Single Inheritance - In this type of inheritance, a class can inherit from
only one class. In Dart, we can only extend one class at a time.

2. Multilevel Inheritance - In this type of inheritance, a class can inherit


from another class and that class can also inherit from another class.
In Dart, we can extend a class from another class which is already
extended from another class.

3. Hierarchical Inheritance - In this type of inheritance, parent class is


inherited by multiple subclasses. For example, the Car class can be
inherited by the Toyota class and Honda class.

4. Multiple Inheritance - In this type of inheritance, a class can inherit


from multiple classes. Dart does not support multiple inheritance. For
e.g. Class Toyota extends Car, Vehicle {} is not allowed in Dart.
Multilevel Inheritance In Dart
Output : Name: Tesla Model 3
Price: 50000.0
Color: Red
Hierarchical Inheritance In Dart
Output : Area of the rectangle: 200.0
Area of the triangle: 100.0
Polymorphism in Dart
u 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.
u In the real world, polymorphism is updating or modifying the feature, function, or
implementation that already exists in the parent class.
u Output :Animal is eating
u Dog is eating
Abstract Class in Dart
u Abstract classes are classes that cannot be initialized.
u It is used to define the behavior of a class that can be inherited by
other classes.
u An abstract class is declared using the keyword abstract.
u An abstract method is a method that is declared without an
implementation .
u You can’t create an object of an abstract class.
u It can have both abstract and non-abstract methods.
u It is used to define the behavior of a class that other classes can
inherit.
u Abstract method only has a signature and no implementation.
Output : Car started
Car stopped
Bike started
Bike stopped
Thanks !

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy