Classes vs Objects
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 4 of 110.
Classes and objects serve different purposes. A class is a blueprint, while an object is what you build from that blueprint.
Here is an example of a class with instance variables:
class Dog {
String name = '';
String breed = '';
}The variables declared inside a class (like name and breed) belong to each object created from that class. We will explore instance variables in depth later.
Create multiple objects from the same class:
Dog dog1 = Dog();
Dog dog2 = Dog();Each object can have its own unique values for those variables:
dog1.name = 'Nicky';
dog1.breed = 'Siberian Husky';
dog2.name = 'Teemon';
dog2.breed = 'Labrador';Use the dot operator . to access an object's variables:
print('${dog1.name} is a ${dog1.breed}');
print('${dog2.name} is a ${dog2.breed}');Output:
Nicky is a Siberian Husky
Teemon is a LabradorKey Difference: The class Dog defines the structure all dogs share, while dog1 and dog2 are individual objects with their own unique data.
Challenge
EasyComplete the code to create two student objects from the Student class and set their variables.
student.dart: Contains theStudentclass (locked)driver.dart: Create two objects and set their data
Set the following values:
student1: name is'Alice'with grade'A'student2: name is'Bob'with grade'B'
Cheat sheet
A class is a blueprint that defines the structure and behavior of objects. An object is an instance created from a class.
Define a class with instance variables:
class Dog {
String name = '';
String breed = '';
}Create objects from a class:
Dog dog1 = Dog();
Dog dog2 = Dog();Access and modify object variables using the dot operator .:
dog1.name = 'Nicky';
dog1.breed = 'Siberian Husky';
dog2.name = 'Teemon';
dog2.breed = 'Labrador';Access object variables:
print('${dog1.name} is a ${dog1.breed}');
// Output: Nicky is a Siberian HuskyTry it yourself
import 'student.dart';
void main() {
// TODO: Create two Student objects
// TODO: Set name and grade for student1 (name: 'Alice', grade: 'A')
// TODO: Set name and grade for student2 (name: 'Bob', grade: 'B')
print('${student1.name} has grade ${student1.grade}');
print('${student2.name} has grade ${student2.grade}');
}
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