Boss fight

Section Exercises

Practice Object-Oriented Programming concepts

Apply your knowledge of classes, inheritance, and OOP principles through these practical exercises.

0 / 13 cleared

Cleared0%
1

Classes & Objects

Create and work with classes

1

Challenge 1

Easy

Create a Person class with name and age properties, and a method introduce() that prints "Hi, I'm [name] and I'm [age] years old".

starter.dart
1// Your class here
2

Challenge 2

Easy

What does the "this" keyword refer to in a class constructor?

Your answer
3

Challenge 3

Medium

Add a named constructor to the Person class called Person.guest() that sets name to "Guest" and age to 0.

starter.dart
1class Person {
2 String name;
3 int age;
4
5 Person(this.name, this.age);
6
7 // Add named constructor here
8}
4

Challenge 4

Medium

Create a BankAccount class with a private _balance property and methods deposit() and getBalance().

starter.dart
1// Your class here
5

Challenge 5

Medium

Why should you make class properties private?

Your answer
6

Challenge 6

Medium

Add a static method to a MathHelper class called square() that returns the square of a number.

starter.dart
1class MathHelper {
2 // Add static method here
3}
2

Inheritance

Extend classes and override methods

1

Challenge 1

Medium

Create a Student class that extends Person and adds a studentId property.

starter.dart
1class Person {
2 String name;
3 int age;
4 Person(this.name, this.age);
5}
6 
7// Your Student class here
2

Challenge 2

Medium

What does the "super" keyword do in a child class constructor?

Your answer
3

Challenge 3

Medium

Override the introduce() method in Student to also print the student ID.

starter.dart
1class Person {
2 String name;
3 int age;
4 Person(this.name, this.age);
5
6 void introduce() {
7 print("Hi, I'm \$name");
8 }
9}
10 
11class Student extends Person {
12 String studentId;
13 Student(String name, int age, this.studentId) : super(name, age);
14
15 // Override introduce() here
16}
4

Challenge 4

Hard

Create an abstract class Animal with an abstract method makeSound(), then create a Dog class that implements it.

starter.dart
1// Your code here
3

Interfaces & Mixins

Use interfaces and mixins for code reuse

1

Challenge 1

Medium

Create a Drawable interface with a draw() method, then create a Circle class that implements it.

starter.dart
1// Your code here
2

Challenge 2

Hard

What is the main difference between "extends" and "implements"?

Your answer
3

Challenge 3

Hard

Create a mixin called Flyable with a method fly(), then use it in a Bird class.

starter.dart
1// Your code here

Loot collected

Key takeaways
  • OOP helps organize code into reusable components
  • Inheritance allows code reuse and extension
  • Encapsulation protects data and hides implementation details