Practice Object-Oriented Programming concepts
Apply your knowledge of classes, inheritance, and OOP principles through these practical exercises.
0 / 13 cleared
Create and work with classes
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".
1// Your class hereWhat does the "this" keyword refer to in a class constructor?
Add a named constructor to the Person class called Person.guest() that sets name to "Guest" and age to 0.
1class Person {2 String name;3 int age;4 5 Person(this.name, this.age);6 7 // Add named constructor here8}Create a BankAccount class with a private _balance property and methods deposit() and getBalance().
1// Your class hereWhy should you make class properties private?
Add a static method to a MathHelper class called square() that returns the square of a number.
1class MathHelper {2 // Add static method here3}Extend classes and override methods
Create a Student class that extends Person and adds a studentId property.
1class Person {2 String name;3 int age;4 Person(this.name, this.age);5}6 7// Your Student class hereWhat does the "super" keyword do in a child class constructor?
Override the introduce() method in Student to also print the student ID.
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() here16}Create an abstract class Animal with an abstract method makeSound(), then create a Dog class that implements it.
1// Your code hereUse interfaces and mixins for code reuse
Create a Drawable interface with a draw() method, then create a Circle class that implements it.
1// Your code hereWhat is the main difference between "extends" and "implements"?
Create a mixin called Flyable with a method fly(), then use it in a Bird class.
1// Your code here