OOP & Dart
OOP & 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.');
}
}
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;
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.');
}
}
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();
}
@override
void sleep() {
print('Curling up in a ball.');
}
}
@override
void sleep() {
print('Perched on a windowsill.');
}
}
void main() {
runApp(MyApp());
}
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.
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.
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.
),
);
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:
• runApp() - Starts the Flutter application and displays the root widget.
• Center - A widget that centers its child widget within its parent widget.
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.
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.
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.
1. The Dart code is compiled into native machine code for the target platform.
6. The Flutter engine routes the events to the appropriate widgets in the widget tree.