Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views40 pages

Object-Oriented Programming in Flutter

The document provides an overview of Object-Oriented Programming (OOP) concepts in Flutter, including classes, objects, encapsulation, inheritance, polymorphism, abstraction, mixins, interfaces, and method overriding. It explains the benefits of OOP in enhancing code reusability and maintenance, along with practical examples and syntax for each concept. The summary highlights the key features of OOP such as data protection, class hierarchies, and flexible behavior through polymorphism.

Uploaded by

JIBIN JIJO
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)
4 views40 pages

Object-Oriented Programming in Flutter

The document provides an overview of Object-Oriented Programming (OOP) concepts in Flutter, including classes, objects, encapsulation, inheritance, polymorphism, abstraction, mixins, interfaces, and method overriding. It explains the benefits of OOP in enhancing code reusability and maintenance, along with practical examples and syntax for each concept. The summary highlights the key features of OOP such as data protection, class hierarchies, and flexible behavior through polymorphism.

Uploaded by

JIBIN JIJO
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/ 40

Object-Oriented

Programming in Flutter
Contents
1. Introduction to OOP in Flutter
2. Class and Object
3. Encapsulation
4. Inheritance (Types included)
5. Polymorphism
6. Abstraction
7. Mixins
8. Interfaces
9. Method Overriding
10. Summary
Introduction to OOP in Flutter

OOP: Organizes code with objects and classes.


Benefits in Flutter: Enhances code reusability,
readability, and maintenance.
Key OOP Concepts:
❖ Class & Object
❖ Encapsulation
❖ Inheritance (with types)
❖ Polymorphism
❖ Abstraction
❖ Mixins
❖ Interfaces
❖ Method Overriding
Class and Object

● Class: A blueprint to create objects with


properties and methods.
● Object: An instance of a class with specific
values.
● Example: Class Car with attributes color and
speed.
Class and Object

Objects Class Class Student

name
age
introduce()

Martin Jenna
Age : 15 Age: 14 Anna
Age: 15
Syntax :
class Student {
// Properties
String name = '';
int age = 0;

// Method
void introduce() {
print("Hi, I am $name and I am $age years old.");
}
}
Creating an Object
void main() {
// Creating a student object
Student student1 = Student();

// Setting properties
student1.name = "Anna";
student1.age = 15;

// Calling the method


student1.introduce(); // Output: Hi, I am Anna and I am 15 years old.
}
Constructor
A constructor is a special method in a class that is automatically called when an object of that
class is created. Its primary purpose is to initialize the properties (variables) of the class and
set up the object with an initial state.

Key Points:

● A constructor has the same name as the class.


● It does not have a return type (not even void).
● Constructors are called with the new keyword (optional in Dart) when creating an
instance of a class, although using new is not necessary.

Purpose of a Constructor:

1. Initialize properties:Use this.property syntax in the constructor to initialize the


properties with the parameter values.
2. Set up the object: Ensure that the object is ready for use right after it's instantiated.
Types of constructor

❖ Default Constructor
❖ Parameterized Constructor
❖ Named Constructor
❖ Factory Constructor
1. Default Constructor
A constructor that does not take any parameters.

class GreetingCard {
// Default constructor
GreetingCard();
void greet() => print('Hello, World!');
}
void main() {
GreetingCard().greet();
}
2. Parameterized Constructor
A constructor that takes parameters to customize the widget.

class GreetingCard {
final String message;
GreetingCard(this.message); // Parameterized constructor
void greet() => print(message);
}
void main() {
GreetingCard("Hello, Dart!").greet(); // Output: Hello, Dart!
}
3. Named Constructor
A constructor with a specific name other than the class name.

class GreetingCard {
final String message;

// Named constructor
GreetingCard.defaultGreetinggg(this.message);
// GreetingCard(this.message); // Regular constructor
void greet() => print(message);
}
void main() {
GreetingCard.defaultGreetinggg("Hello, Dart!").greet(); // Output: Hello, Dart!
}
4. Factory Constructor
A constructor that can return an instance of the class or a subclass.

class Student {
final String name;
// Regular constructor
Student(this.name);
// Factory constructor
factory Student.create(String name) {
if (name.isEmpty) {
return Student("Unknown Student");
} else {
return Student(name);
}}
void showName() => print("Student Name: $name");
}
void main() {
// Without storing the object in a variable
Student.create("").showName(); // Output: Student Name: Unknown Student
Student.create("Alice").showName(); // Output: Student Name: Alice
}
Encapsulation
● Encapsulation is the concept of hiding internal details of a class and protecting its data from
being accessed or modified directly from outside the class.

● Data protection: Controlled access through getters and setters.


○ Private: Use _variableName to restrict access to the class.
○ Public: Accessible from anywhere; default in Dart.
○ Protected: Not directly supported in Dart, but can be emulated using private
properties in a superclass

● Example: User class with private properties and controlled access.


Public:

class User {
String email; // Public property
User(this.email);
}

void main() {
User user = User("[email protected]");
print(user.email); // Accessing public property
}
Private:
class Person {
String _name; // Private property

Person(this._name);

void sayHello() {
print('Hello, my name is $_name');
}
}

void main() {
Person p = Person('Alice');
p.sayHello(); // Output: Hello, my name is Alice
}
Inheritance
● Inheritance: Allows a class to inherit attributes/methods from another class.
● Types of Inheritance:
○ Single Inheritance: One parent, one child class.
○ Multilevel Inheritance: Chain of inheritance (Parent -> Child ->
Sub-child).
○ Hierarchical Inheritance: Multiple classes inherit from one parent.
● Example: Person as a base class and Student as a derived class.
Types of Inheritance
Single Inheritance

Single Inheritance: One derived class inherits from a single base class.

Example: Person as a base class and Student as a derived class


Syntax:
class Person {
String name;

Person(this.name);
}

class Student extends Person {


String studentId;

Student(String name, this.studentId) : super(name);


}

void main() {
Student student = Student("Alice", "S123");
print("Name: ${student.name}, ID: ${student.studentId}");
}
Multilevel Inheritance

Multilevel Inheritance: A chain of inheritance from a


grandparent to parent to child.

Example: Granfather -> Father -> Son.


Syntax:
class Animal {
void sound() => print("Animal sound");
}

class Mammal extends Animal {


void walk() => print("Mammal walks");
}

class Dog extends Mammal {


void bark() => print("Dog barks");
}

void main() {
Dog dog = Dog();
dog.sound();
dog.walk();
dog.bark();
}
Hierarchical Inheritance

● Hierarchical Inheritance: Multiple classes inherit from a


single parent class.
● Example: Vehicle as a base class with Car and Bike as
derived classes.
Syntax:
class Vehicle {
void start() => print("Vehicle starts");
}

class Car extends Vehicle {


void drive() => print("Car drives");
}

class Bike extends Vehicle {


void ride() => print("Bike rides");
}

void main() {
Car car = Car();
car.start();
car.drive();

Bike bike = Bike();


bike.start();
bike.ride();
}
Polymorphism

● Polymorphism: Allows different classes to be treated as the same


base type.
● Enables method overriding for unique behavior in subclasses.
● Example: Base class Shape with subclasses Circle and Rectangle.
Syntax:
class Shape {
void draw() => print("Drawing shape");
}

class Circle extends Shape {


@override
void draw() => print("Drawing circle");
}

class Rectangle extends Shape {


@override
void draw() => print("Drawing rectangle");
}

void main() {
Shape shape1 = Circle();
Shape shape2 = Rectangle();
shape1.draw();
shape2.draw();
}
Abstraction

● Abstraction: Hides unnecessary details, focusing on essential parts.

● Achieved with abstract classes and abstract methods.

● Example: Abstract class Vehicle with an abstract move() method.


Syntax:
abstract class Vehicle {
void move();
}

class Car extends Vehicle {


@override
void move() => print("Car is moving");
}

void main() {
Vehicle car = Car();
car.move();
}
Mixins

● Mixins: Reusable code blocks that share functionality between


classes.
● Useful for adding methods to multiple classes without inheritance.
● Example: FlyMixin used in both Bird and Airplane.
Syntax:
mixin FlyMixin {
void fly() => print("Flying high!");
}

class Bird with FlyMixin {}


class Airplane with FlyMixin {}

void main() {
Bird().fly();
Airplane().fly();
}
Interfaces

● Interfaces: Define method signatures without


implementation.
● In Dart, any class can act as an interface.
● Example: Vehicle interface implemented by Car and Bike.
Syntax:
class Vehicle {
void move();
}

class Car implements Vehicle {


@override
void move() => print("Car is moving");
}

class Bike implements Vehicle {


@override
void move() => print("Bike is moving");
}

void main() {
Vehicle car = Car();
Vehicle bike = Bike();
car.move();
bike.move();
}
Method Overriding

● Method Overriding: Allows subclasses to provide a


specific implementation of a method.
● Used for polymorphism.
● Example: Animal base class with overridden sound()
method in subclasses.
Syntax:
class Animal {
void sound() => print("Animal sound");
}

class Dog extends Animal {


@override
void sound() => print("Bark");
}

class Cat extends Animal {


@override
void sound() => print("Meow");
}

void main() {
Animal myDog = Dog();
Animal myCat = Cat();
myDog.sound();
myCat.sound();
}
Summary

● Encapsulation: Protects data with private fields and public


methods.
● Inheritance: Enables reusability and hierarchy.
○ Single, Multilevel, and Hierarchical Inheritance explained.
● Polymorphism: Supports flexible behavior.
● Abstraction: Focuses on essential details.
● Mixins: Adds functionality to multiple classes.
● Interfaces: Define a contract for implementing classes.
● Method Overriding: Provides specific behavior in subclasses.
Thank You!

You might also like