Flutter
Coding and Programming
01
OOP
Object-Oriented Programming
1-1 OOPS
Object-Oriented Programming
(OOP) is a key paradigm in Dart, just
like in many other languages such as
Java, C#, and Python. Dart supports
OOP concepts such as classes,
objects, inheritance,
polymorphism, abstraction, and
encapsulation. Let’s explore these
key concepts in Dart.
1-2 Classes and Objects Example
Class: A class is a blueprint for creating objects. It defines properties (fields) and methods (functions) that the objects created from it will have.
Object: An object is an instance of a class. Once a class is defined, you can create multiple objects based on that class.
class Person {
String name;
int age;
// Constructor
Person(this.name, this.age);
// Method
void introduce() {
print("Hello, my name is $name and I am $age years old.");
}
}
void main() {
// Creating an object of the Person class
Person person1 = Person('John', 25);
person1.introduce(); // Output: Hello, my name is John and I am 25 years old.
}
1-3 Encapsulation
Encapsulation is a fundamental
concept of Object-Oriented
Programming (OOP), including in Dart. It
refers to bundling the data (variables)
and methods (functions) that operate on
the data within a single unit, usually a
class, and restricting direct access to
some of the object’s components.
Encapsulation allows you to control
access to the data by exposing only
necessary parts through methods or
getters and setters, while keeping other
parts private.
1-4 Encapsulation Example
class BankAccount {
// Private field
double _balance = 0;
// Method to deposit money
void deposit(double amount) {
_balance += amount;
}
// Method to check balance
double getBalance() {
return _balance;
}
}
void main() {
BankAccount account = BankAccount();
account.deposit(1000);
print("Balance: ${account.getBalance()}");
// Output: Balance: 1000
}
1-5 Inheritance
Inheritance is a key concept in object-oriented programming (OOP),
where a class can inherit properties and methods from another class.
The class that inherits is called a subclass (or child class), and the class
being inherited from is the superclass (or parent class).
1-6 Inheritance Example
class Animal {
void makeSound() {
print('Animal makes a sound');
}
}
class Dog extends Animal {
@override
void makeSound() {
print('Dog barks');
}
}
void main() {
Dog dog = Dog();
dog.makeSound(); // Output: Dog barks
}
1-7 Inheritance Types
1-8 Polymorphism
Dart doesn’t have explicit interfaces like some other languages, but any class
can be treated as an interface. A class can implement multiple interfaces, and
this is a form of polymorphism where different classes can implement the same
interface in different ways.
1-9 Polymorphism Example
class Animal { void main() {
void makeSound() { Animal animal;
print('Animal makes a
sound'); animal = Dog();
} animal.makeSound(); // Output:
} Dog barks (Runtime
polymorphism)
class Dog extends Animal {
@override animal = Cat();
void makeSound() { animal.makeSound(); // Output:
print('Dog barks'); Cat meows (Runtime
} polymorphism)
} }
class Cat extends Animal {
@override
void makeSound() {
print('Cat meows');
}
}
1-10 Abstraction
abstraction is a fundamental concept in object-oriented programming (OOP) that
allows you to hide the internal implementation details of a class and expose only
the relevant functionalities to the user. This is achieved by defining abstract
classes and abstract methods that provide a blueprint for other classes to
implement.
3-2 Abstraction Example
abstract class Animal { void main() {
void makeSound(); // Abstract method Animal animal;
}
animal = Dog();
class Dog extends Animal { animal.makeSound(); // Output: Dog barks
@override
void makeSound() { animal = Cat();
print('Dog barks'); animal.makeSound(); // Output: Cat meows
} }
}
class Cat extends Animal {
@override
void makeSound() {
print('Cat meows');
}
}
Thanks
Do you have any
questions?
!
Resources
https://flutter.dev