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

OOP & Dart

The document discusses object-oriented programming concepts and their implementation in Dart. It covers core OOP concepts like classes, objects, inheritance, encapsulation, polymorphism and abstraction. It also provides an example of a simple "Hello World" Flutter app that demonstrates how these concepts are used to build user interfaces with Dart and Flutter.

Uploaded by

Koken2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
681 views

OOP & Dart

The document discusses object-oriented programming concepts and their implementation in Dart. It covers core OOP concepts like classes, objects, inheritance, encapsulation, polymorphism and abstraction. It also provides an example of a simple "Hello World" Flutter app that demonstrates how these concepts are used to build user interfaces with Dart and Flutter.

Uploaded by

Koken2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Notes on: Object Oriented Programming & Dart

Object-oriented programming (OOP) is a programming paradigm that organizes code around objects,
which encapsulate data (attributes) and behavior (methods). OOP aims to model real-world entities and
their interactions, making code more modular, reusable, and maintainable. OOP is defined as a
programming paradigm that relies on the concept of classes and objects.
Core Concepts of OOP
An object is a self-contained entity that consists of data and behavior. Data is represented by the object's
attributes, and behavior is represented by the object's methods.

Classes: Classes are blueprints for creating objects. They define the attributes and methods that an
object will have. A class is like a mold, and an object is like a cookie made from that mold.
class Dog {
String name;
int age;

void bark() {
print('Woof!');
}
}

Objects: Objects are instances of classes. They have their own set of values for the attributes defined in
the class. An object is like a specific dog, with its own name, age, and behavior.
Dog myDog = new Dog();
myDog.name = 'Fido';
myDog.age = 1;
myDog.bark();

Inheritance: Inheritance allows classes to inherit attributes and methods from parent classes. This
enables code reuse and specialization. A subclass can inherit the functionality of its parent class and
extend it with its own unique features.
class Animal {
String name;
int age;

void eat() {
print('I am eating.');
}
}

class Dog extends Animal {


String breed;

void bark() {
print('Woof!');
}
}

Encapsulation: Encapsulation is the process of hiding data and controlling how it is modified. This
promotes information hiding, making code more secure and maintainable. By restricting direct access to
an object's data, encapsulation ensures that changes are made through controlled methods.
class BankAccount {
// Encapsulated data
double balance;

// Method to deposit money


void deposit(double amount) {
balance += amount;
}

// Method to withdraw money


bool withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
return true;
} else {
return false;
}
}
}

Polymorphism: Polymorphism enables objects of different classes to respond to the same message in
different ways, depending on their specific type. This allows for flexible and adaptable code. A method
can have different implementations for different classes that inherit from a common parent class.
class Shape {
void draw() {
print('Drawing a shape.');
}
}

class Circle extends Shape {


@override
void draw() {
print('Drawing a circle.');
}
}

class Square extends Shape {


@override
void draw() {
print('Drawing a square.');
}
}

Polymorphism is the ability of objects to take on different forms. This is achieved through method
overriding and overloading.
class Dog {
String name;
int age;

void bark() {
print('Woof!');
}
}
class Poodle extends Dog {
bool isCurlyHaired;
}

Method overriding: Method overriding is when a subclass overrides a method from its superclass. This
allows the subclass to provide its own implementation of the method.

Example: The Poodle class could override the bark() method from the Dog class to print a different
sound, such as "Woof, woof!"

Method overloading: Method overloading is when a class has two or more methods with the same
name, but with different parameters. This allows the class to provide different implementations of the
method depending on the parameters.

Example: The Dog class could have two methods called bark(), one with no parameters and one with a
parameter for the volume of the bark. This would allow the class to bark at different volumes.

Abstraction: Abstraction focuses on the essential characteristics of an object, hiding the underlying
implementation details. It simplifies code complexity and promotes reusability. Abstraction allows us to
create interfaces that define the essential methods of a class, without specifying the implementation
details.
abstract class Animal {
void eat();
void sleep();
}

class Dog implements Animal {


@override
void eat() {
print('Eating kibble.');
}

@override
void sleep() {
print('Curling up in a ball.');
}
}

class Cat implements Animal {


@override
void eat() {
print('Lapping up milk.');
}

@override
void sleep() {
print('Perched on a windowsill.');
}
}

A simple “Hello World” app code:


import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
home: Center(
child: Text('Hello, world!'),
),
);
}
}

Explanation:
import 'package:flutter/material.dart';

This line imports the material package from the Flutter SDK. The material package contains all of the
widgets that are used to build the user interface of a Flutter application.

void main() {

The main() function is the entry point for the Flutter application. This is where the program starts
execution.

runApp(MyApp());
The runApp() function starts the Flutter application and displays the root widget. The root widget is the
top-level widget in the application, and it is responsible for building the entire user interface.

class MyApp extends StatelessWidget {

The MyApp class is a custom widget that represents the root widget of the application.

@override

The @override annotation tells the Dart compiler that the build() method is overriding a method from
another class.

Widget build(BuildContext context) {

The build() method is responsible for building the user interface of the widget. It takes a BuildContext
object as its parameter, which provides information about the widget's environment.

return MaterialApp(

The MaterialApp widget is the root widget of all Flutter applications. It provides the basic infrastructure
for building a Flutter application, such as the theme, routing, and localization.

home: Center(

The Center widget centers its child widget within its parent widget.

child: Text('Hello, world!'),

The Text widget displays text on the screen.

),

The closing parentheses for the MaterialApp and Center widgets.

);

The closing parentheses for the main() function and the MyApp class.

To summarize, the example code creates a simple Flutter application that displays the text "Hello,
world!" in the center of the screen. The code uses the following terms and functions:

• main() - The entry point for the Flutter application.

• runApp() - Starts the Flutter application and displays the root widget.

• MaterialApp - The root widget of all Flutter applications.

• Center - A widget that centers its child widget within its parent widget.

• Text - A widget that displays text on the screen.


Q. Can there be multiple main() function in a single program?

A. No, there can only be one main() function in a single program. The main() function is the entry point
for the program, and it is where the program starts execution. If you have multiple main() functions in a
single program, the compiler will give you an error.

There are a few workarounds for this limitation. For example, you could have multiple main() functions in
different files, and then use a linker to link the files together. However, this is not a recommended
approach, as it can make your code more difficult to maintain.

A better approach is to use a single main() function and then use functions and classes to organize your
code. This will make your code more modular and easier to maintain.

Here is an example of how to use functions and classes to organize code:


void main() {
// Create a new instance of the MyApp class.
MyApp app = MyApp();

// Call the app's build() method to create the user interface.


app.build();
}

class MyApp {
Widget build(BuildContext context) {
// Return a MaterialApp widget.
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My Flutter App'),
),
body: Center(
child: Text('Hello, world!'),
),
),
);
}
}

In this example, the main() function simply creates a new instance of the MyApp class and then calls its
build() method. The build() method is responsible for creating the user interface.

This approach allows to have multiple functions and classes in program, but it still ensures that there is
only one main() function.

Q. How does a Flutter program work?

A. A Flutter program works by compiling the Dart code into native machine code for the target platform.
This machine code is then executed by the Flutter engine, which is a cross-platform runtime environment
for Flutter applications.
The Flutter engine is responsible for rendering the user interface, handling user input, and managing the
state of the application. It also provides a number of services that Flutter applications can use, such as
networking, storage, and sensors.

When a Flutter application is launched, the Flutter engine creates a widget tree. The widget tree is a
hierarchical representation of the user interface. The Flutter engine then renders the widget tree to the
screen.

When the user interacts with the user interface, the Flutter engine generates events. These events are
then routed to the appropriate widgets in the widget tree. The widgets then handle the events and
update their state.

The Flutter engine also manages the state of the application. The state of the application includes things
like the user's current location, the data that is displayed in the user interface, and the application's
settings.

The following is a simplified overview of how a Flutter program works:

1. The Dart code is compiled into native machine code for the target platform.

2. The Flutter engine executes the machine code.

3. The Flutter engine creates a widget tree.

4. The Flutter engine renders the widget tree to the screen.

5. The Flutter engine handles user input and generates events.

6. The Flutter engine routes the events to the appropriate widgets in the widget tree.

7. The widgets handle the events and update their state.

8. The Flutter engine updates the state of the application.

9. The process repeats from step 5.

This is a simple overview.

You might also like