Mastering Dart Programming: Understanding OOP with Classes
Written on
Introduction to Object-Oriented Programming
Object-oriented programming (OOP) is a paradigm focused on objects, which encapsulate both data and behavior. In Dart, objects are defined through classes. In this tutorial, we'll create an object that embodies various superpowers.
Properties in Dart
A property in Dart is essentially a variable defined within a class.
Example:
class SuperPowers {
String powerOne = "Super Strength";
String powerTwo = "Super Speed";
String powerThree = "Flight";
}
void main() {
SuperPowers showPower = new SuperPowers();
print(showPower.powerThree);
}
The output will display: Flight. Here, we utilized properties to define various superpowers and printed one of them by calling the SuperPowers class.
Methods Explained
A method is akin to a function that resides within a class.
Example:
class SuperPowers {
String powerOne = "Super Strength";
String powerTwo = "Super Speed";
String powerThree = "Flight";
int numberOfPowers = 3;
String learnNewPowers(String newPower) {
return newPower;}
}
void main() {
SuperPowers superPowers = new SuperPowers();
String newPower = superPowers.learnNewPowers("Heat Vision");
print("I learned $newPower");
}
The output will be: I learned Heat Vision. In this scenario, we enhanced our SuperPowers class with a method that enables the learning of new abilities.
Constructors in Dart
A constructor operates similarly to a method but is invoked automatically when a class is instantiated. In Dart, a constructor shares the name with its class.
Example:
class SuperPowers {
String powerOne = "Super Strength";
String powerTwo = "Super Speed";
String powerThree = "Flight";
int numberOfPowers = 3;
SuperPowers(int num) {
this.numberOfPowers = num;
print("$num powers so far.");
}
}
void main() {
SuperPowers superPowers = new SuperPowers(3);
}
The output will read: 3 powers so far. The this keyword refers to the current class instance.
Inheritance in Dart
Inheritance enables a class to inherit properties and methods from another class, promoting code reusability. The parent class is termed the superclass, while the inheriting class is the subclass.
Example:
class SuperPowers {
String powerOne = "Super Strength";
String powerTwo = "Super Speed";
String powerThree = "Flight";
int numberOfPowers = 3;
SuperPowers() {
print("Superpowers granted");}
String learnNewPowers(String newPower) {
return newPower;}
}
class Person extends SuperPowers {
String knownQuality = "Brave";
}
void main() {
Person newHero = new Person();
String knownQuality = newHero.knownQuality;
print("Our new hero is $knownQuality");
}
The output will show: Superpowers granted followed by Our new hero is Brave. This example illustrates how the subclass Person utilizes its method while inheriting from the superclass SuperPowers.
Method Overriding
You can modify an existing method from the superclass in the subclass; this is known as method overriding.
Example:
class SuperPowers {
String powerOne = "Super Strength";
String powerTwo = "Super Speed";
String powerThree = "Flight";
int numberOfPowers = 3;
SuperPowers() {
print("Superpowers granted");}
String learnNewPowers(String newPower) {
return newPower;}
}
class Person extends SuperPowers {
String learnNewPowers(String newPower) {
print("Our new hero learns $newPower");
return newPower;
}
}
void main() {
Person newHero = new Person();
String learnedPower = newHero.learnNewPowers("Frost Breath");
}
The output will display: Superpowers granted and Our new hero learns Frost Breath.
In this video, "Object Oriented Programming and Classes in the Dart Programming Language - Dart Tutorial Part 2," you will gain insights into how Dart implements OOP principles effectively.
This full course titled "Dart OOP Full Course for Beginners - Learn Dart Programming in 2023" serves as a comprehensive guide to mastering Dart programming with a focus on OOP concepts.
May the code be with you,
- Arc