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

Dart OOP

The document outlines the fundamental rules of Object-Oriented Programming (OOP) in Dart, including classes, objects, constructors, methods, inheritance, and null safety features. It emphasizes the importance of null safety, required parameters, and the use of various operators and keywords such as late, final, and const. Additionally, it covers advanced concepts like interfaces, mixins, and operator overloading, providing a comprehensive overview of Dart's OOP principles.
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 views11 pages

Dart OOP

The document outlines the fundamental rules of Object-Oriented Programming (OOP) in Dart, including classes, objects, constructors, methods, inheritance, and null safety features. It emphasizes the importance of null safety, required parameters, and the use of various operators and keywords such as late, final, and const. Additionally, it covers advanced concepts like interfaces, mixins, and operator overloading, providing a comprehensive overview of Dart's OOP principles.
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/ 11

Dart OOP Rules

1. Classes & Objects

• Classes are defined with class.

• Objects are created using constructors.

• Every class inherits from Object (unless it extends another class).

2. Fields & Null Safety

• By default, variables must be initialized.

• Non-nullable types must be assigned before use.

• Use ? to make a field nullable.

• Use late for non-nullable fields initialized later.

• Use required for constructor parameters.

3. Constructors

• Default constructor: ClassName()

• Named constructors: ClassName.named()

• Redirecting constructors: call another constructor

• Initializer list: : field = value


4. Methods

• Instance methods belong to objects.

• Static methods belong to the class (use static).

• Getters/setters provide encapsulation.

5. Inheritance

• Use extends for single inheritance.

• super calls the parent constructor/method.


6. Abstract Classes & Methods

• Cannot be instantiated.

• Must be extended.

• Can contain abstract methods (no body).

7. Interfaces

• Every class in Dart defines an interface automatically.

• Use implements to enforce contracts.

• Must override all members.

8. Mixins

• Share functionality between classes without inheritance.

• Use with.
9. Polymorphism & Overriding

• Overriding is allowed with @override.

• Covariant return types are allowed.

• Dart does not support method overloading → use optional/named params instead.

10. Encapsulation

• Use _ (underscore) for private members (library-private).

• Use getters/setters to expose data safely.

11. Static Members


• Belong to the class, not instances.

• static fields/methods cannot access instance members.

12. Null Safety Features (since 2.12)

• Non-nullable by default: String name = "Bob";

• Nullable types: String? nickname;

• Null-aware operators:

o ?? (default value)

o ?. (safe access)

o ??= (assign if null)

• Late variables: initialized later, but non-nullable.

• Required named parameters:

13. Final & Const

• final: set once at runtime.

• const: compile-time constant.


14. Operator Overloading

• You can override operators using operator.

15. Type System

• Strongly typed, but has type inference (var, final, const).

• Generics supported.

Summary of Key Dart OOP + Null Safety Rules

1. Classes & objects → class, everything extends Object.

2. Null safety → ?, late, required.

3. Constructors → default, named, redirecting, initializer lists.

4. Methods → instance, static, getters, setters.

5. Inheritance → single (extends), use super.

6. Abstract classes & methods.

7. Interfaces → any class can be implements.

8. Mixins → with.

9. Overriding allowed, overloading not allowed.

10. Encapsulation → _ for private, getters/setters.

11. Static members → class-level only.

12. Null-aware operators (??, ?., ??=).

13. Final vs const.

14. Operator overloading with operator.

15. Generics supported.


1. Required Parameters

Positional Required

By default, positional parameters in constructors/methods are required:

• Declared inside () normally.


• Required by default → the caller must pass them.

Named Parameters

• Named parameters are optional by default and declared inside {}.

• To make them required, add required.

• Caller must use the parameter name when calling.

2. Default Values

• Can be given for optional positional ([]) and optional named ({}) parameters.
• Must be a constant value at compile time.
Without a default value, the parameter must be nullable (?) or marked required.

3. Optional Parameters

Two kinds:

Optional Positional ([]): Caller can omit them → they default to null (if nullable) or to a default value
you provide.

Optional Named ({})


4. late Keyword

• For non-nullable variables initialized later.

• Useful for dependency injection, initialization after construction, or expensive values.

Must assign before use, otherwise runtime error.

5. Nullable vs Non-Nullable

• By default, variables are non-nullable.

• Add ? to allow null.

6. Null-aware Operators

• ?? → provide fallback

• ??= → assign only if null

• ?. → call safely
7. required + Nullable

Yes, you can make a parameter required and nullable:

8. Constant Constructors & Null Safety

If you use const, all fields must be final and non-nullable unless given defaults.

• Positional params → required by default.


• Named params → optional by default, add required to enforce.

• Default values → only for optional parameters (named/positional).

• late → non-nullable, assigned later.

• ? → type is nullable.

• Null-aware ops → ??, ??=, ?..

• required works with both nullable & non-nullable.

• const constructors → all fields must be final, and either non-nullable with initializer or nullable.

You might also like