September 2019
Beginner
512 pages
12h 52m
English
A great common example of overriding parent behavior is the toString() method. The objective of this method is to return a String representation of the object:
class Student extends Person { // ... fullName(from Person class) and other fields @override String toString() => "$fullName, also known as $nickName";}main() { Student student = new Student("Clark", "Kent", "Kal-El"); print("This is a student: $student"); // prints: This is a student: Clark Kent, also known as Kal-El // will also call the toString() of student implicitly}
As you can see, this makes the code cleaner, and we provide a good textual representation of the object that can aid in understanding logs, text formatting, and more.
Read now
Unlock full access