Introduction to OOP
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 3 of 110.
Object-Oriented Programming (OOP) organizes code around objects that contain data (instance variables) and functions (methods).
Create a file called car.dart with a class
class Car {
// empty class body
}Create another file called driver.dart to use the class
import 'car.dart';Create an object from the Car class using the class name followed by parentheses
void main() {
Car myCar = Car();
}Check the type of your object using runtimeType
print(myCar.runtimeType);Output:
CarThis confirms you've successfully created an object from your Car class. In OOP, a class is like a blueprint, and an object is what you build from that blueprint. In Dart, you create objects by calling the class name like a function: Car().
Challenge
MediumIn this challenge, you'll use a Car class defined in car.dart and create an object from it in driver.dart.
Update driver.dart to:
- Create an object from the
Carclass usingCar()and store it in a variable calledmyCar
The driver file will print the runtime type to verify your implementation.
Cheat sheet
Object-Oriented Programming (OOP) organizes code around objects that contain data (instance variables) and functions (methods).
A class is like a blueprint, and an object is what you build from that blueprint.
Define a class:
class Car {
// empty class body
}Import a class from another file:
import 'car.dart';Create an object from a class using the class name followed by parentheses:
Car myCar = Car();Check the type of an object using runtimeType:
print(myCar.runtimeType); // Output: CarTry it yourself
import 'car.dart';
void main() {
// TODO: Create a Car object named myCar
Car myCar = ?;
print(myCar.runtimeType);
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor