#01
Flutter Lecture
By Mudassar Naeem
Flutter Classes and
objects
(Object Oriented
RoadMap of OOP Programming)
Classes and Objects: Object
• Definitions
• How to define/initialize classes
01 • How to create objects Object
• Accessing Class members
• The “this” keyword Blueprint
constructors: Constructor Constructor
• Default constructor
• Parameterized constructor
02 • Constructor initialization
• Constructor Chaining
• Private Constructor
Encapsulation
Encapsulation:
• Access Modifiers
03 • Getters and Setters
• Data Hiding and Security
2
(Object Oriented
RoadMap of OOP Programming)
Inheritance: Inheritance
• Inheritance Types
04 • Method Overriding
• Super Keyword
• Constructor in inheritance
• Object Class
Polymorphism:
Polymorphism
• Polymorphism types
05 • Method Overloading
• Dynamic Method Dispatch
• Covariant Return Type
Abstraction & Interfaces: Abstraction Interfaces
• Definitions
• Difference
06 • Default & static methods in interface
• Functional Interface and Lambda
Expression
3
(Object Oriented
RoadMap of OOP Programming)
Association:
Association
• Definition
07 • Types of Association
o Aggregation
o Composition
Exception Handling: Exception Handling
• Try-Catch Block
• Multiple Catch Blocks
08 • Finally Block
• Checked vs Unchecked Exceptions
• Custom Exceptions
Package: Package
• Creating and Using packages
09 • Importing Packages (internal or external)
• Access modifiers in packages
• Static import
4
(Object Oriented
RoadMap of OOP Programming)
Static Keyword: Message
• Static Variables
10 • Static Methods
• Static Blocks Sender Encoding
• Static Nested Classes
• Singleton Design Patterns using static
Final Keyword:
• Final Variable Transmission
11 • Final Method
• Final Classes
• Immutable Classes Chanel Reveiver
Object Class Methods:
• toString() Feedback
• Equal() and hashCode()
12 • Clone()
• getClass() Barriers
• Finalize() Decoding
5
(Object Oriented
RoadMap of OOP Programming)
Inner Classes:
• Member Inner Class Message
13 • Local Inner Class
• Anonymous Inner Class
• Static Nested Class Sender Encoding
Enums & Annotations:
• Definitions Transmission
14 • Enum methods and constructors
• Built-in annotations
• Custom annotations Chanel Reveiver
SOLID Practices in OOP:
• Single Responsibility Principle Feedback
• Open/Closed Principle
15 • Liskov Substitution Principle
• Interface Segregation Principle Barriers
• Dependency Inversion Principle Decoding
6
Topic#01: Classes and Objects
Class: Object:
Definition: Definition:
• A blueprint or template of real-life • An instance of a class that Represents a
objects. Defines the properties real-world entity with state (data) and
(attributes) and behaviors (methods) behavior (methods).
of objects.
Syntax to Define:
class ClassName { Syntax to Define:
(Attributes) ClassName objectName = ClassName();
(Methods)
}
Example:
class Car { Example:
String color; // Attribute Car myCar = Car();
void drive() { // Method
[Link]("Car is driving!");
}
}
7
Topic#01: Classes and Objects
Class:
Why we create Classes? Elements of Class: Use of “this” keyword:
• To avoid redundancy of code • class: this keyword is used to define a • Refers to the current instance of the
• We use classes when two or more class of a real-life object. class. Used to differentiate between
objects have similar attributes and • className: Represents the name of instance variables and parameters (if
methods. For example, if we need to the class/object. they have the same name). Or Calling
handle the records of cars, the cars • Attributes: defines the properties of one constructor from another
have same attributes and methods object. These are declared as (constructor chaining).
such as: class/object variables.
• Attributes • Methods: defines the functionalities of • Example:
• Color • Make the object or how the functions
operates class Car {
• Variant • Number String color;
• Methods Visual Representation: Car(String color) {
• Drive • Break Class Name [Link] = color;
class Car { }
• Park String color; Attribute }
These Attributes and methods can be void drive() { // Method
same for car objects. So instead of [Link]("Car is driving!"); • Common Uses:
writing the same code for each car } ▪ Assigning values to instance variables in
object, we define a template/blueprint } constructors.
for car object. So all car objects can be ▪ Returning the current object from a
handle by single class. Keyword to define class method.
Method ▪ Passing the current object as a 8
parameter.
Topic#01: Classes and Objects
Object:
How to create Objects? Accessing Class Members:
• Object can be anything. Like a car, • Use the dot (.) operator to access
any animal, plant or anything. In fields and methods of an object.
java, if you want to create an object
of any class, you can create by Syntax:
following the following instructions. [Link]; // Access field
[Link](); // Call method
Syntax:
ClassName objName = ClassName(); Example:
[Link] = “Red”; // Access field
Example:
[Link](); // Call method
Car myCar = Car();
Note:
Steps:
• Members can be accessed only if
• Declaration: Car myCar (declares a they are public or within the same
reference variable). class/package (depending on access
• Initialization: Car() (calls the constructor modifiers).
to initialize the object).
9
Methods in Class
#01
Object:
Concept: Parameter List:
• Methods in class are used to perform any kind In Dart, there are four methods to pass
of operation. They are written to reduce the arguments to a method or constructor. These
redundancy of the code. are as follows
Structure: Positional:
These are the mandatory arguments that must be
Method Name Parameter list
provided in the correct order.
return_type method_name(Parameters){ Named:
method body(statements) Named arguments allow you to specify arguments
return statement if any using their names. They are wrapped in {} and are
} optional by default.
Return Type Method Body
Optional Positional:
Return Type: Positional arguments can be made optional by
• Return type must specify if the method enclosing them in []. If they are not provided, they
returns anything. The type of value default to null.
that return statement returns, is specify Default:
here. It may be “int”, “double”,
“var”, “dynamic”, “class”, You can assign a default value to optional
“widget”, or “widget property” parameters
10
Methods in Class (Parameter Types)
#02
Positional: V/S Optional Positional:
Concept:
Concept: Positional arguments can be made optional by
Positional parameters are the mandatory enclosing them in []. If they are not provided,
parameters that must be provided in the they default to null. They are must be placed
correct order in which they are written. after required ones.
Example: Example:
void greet(String name, int age) { void greet(String name, [int? age]) {
print("Hello, $name!”) if (age != null) {
print(“You are $age years old."); print("Hello, $name!”)
} print(“You are $age years old.");
} else {
void main() { print("Hello, $name!");
greet("Alice", 25); // Works fine }
// greet("Alice"); ERROR: Missing }
argument
} void main() {
greet("Emma", 35); // Works
greet("Emma"); // Works (age is null)
}
11
Methods in Class (Parameters Types)
#03
Named Arguments:
Making name arguments Required:
Concept: Use the required keyword to enforce mandatory
Named arguments allow you to specify named arguments.
arguments using their names. These
arguments can be provided in any order.
They are wrapped in { } and are optional by
default. Example:
Example: void greet({required String? name,
required int? age}) {
void greet({String? name, int? age}) {
print("Hello, $name!”)
print("Hello, $name!”)
print(“You are $age years old.");
print(“You are $age years old.");
}
}
void main() {
void main() {
greet(name: "Alice", age: 25);// Works
greet(name: "Alice", age: 25);// Works
greet(age: 25, name: "Alice");// Works
greet(name: "Ali"); // Works, age=null
greet(name: "Ali"); // Error
greet(age: 25); // Works, name = null
greet(age: 25); // Error
}
}
12
Methods in Class (Parameters Types)
#04
Default Arguments:
Concept:
You can assign a default value to optional
parameters.
Example with named arguments: Example with Optional Positional Arguments:
void greet({String name=“Zia”, int age=18}) void greet({String name, [int age=18]}) {
{ print("Hello, $name!”)
print("Hello, $name!”) print(“You are $age years old.");
print(“You are $age years old."); }
}
void main() {
void main() {
greet("Alice", 25);// Works
greet(name: "Alice", age: 25);// Works greet(name: "Ali");// Works age=18
greet(name: "Ali"); // Works, age=18
greet(age: 25); // Works, name = Zia }
13
Methods in Class (Parameters Types)
#03
Can have
Type Syntax Optional? Example
Default value?
greet("John",
Positional (p1, p2, p3) No No 25)
greet(name:
Named ({p1, p2, p3}) Yes Yes "John", age: 25)
Optional (p1, [p2, p3]) Yes Yes greet("John")
Positional
Default ({p=default}), Yes Yes
greet(name:
Arguments OR "John")
([p2=default])
14