Dart Inheritance
Creating new classes from existing ones
🧬 What is Dart Inheritance?
Inheritance allows creating new classes based on existing ones. Child classes inherit properties and methods from parent classes, promoting code reuse and establishing relationships between classes.
// Parent class
class Animal {
void eat() => print('Animal is eating');
}
// Child class inherits from Animal
class Dog extends Animal {
void bark() => print('Dog is barking');
}
Inheritance Concepts
Parent Class
Base class that provides properties
class Vehicle {
String brand;
void start() => print('Starting');
}
Child Class
Derived class that inherits properties
class Car extends Vehicle {
int doors;
void honk() => print('Beep beep!');
}
Method Override
Child class can modify parent methods
class Bird extends Animal {
@override
void move() => print('Flying');
}
Super Keyword
Access parent class members
class Student extends Person {
Student(String name) : super(name);
}
🔹 Basic Inheritance
Create a child class that inherits from a parent class:
// Parent class
class Person {
String name;
int age;
Person(this.name, this.age);
void introduce() {
print('Hi, I am $name and I am $age years old');
}
void walk() {
print('$name is walking');
}
}
// Child class inheriting from Person
class Student extends Person {
String school;
List subjects = [];
Student(String name, int age, this.school) : super(name, age);
void study(String subject) {
subjects.add(subject);
print('$name is studying $subject');
}
void showSubjects() {
print('$name studies: ${subjects.join(', ')}');
}
}
void main() {
Student student = Student('Alice', 16, 'High School');
// Using inherited methods
student.introduce();
student.walk();
// Using child class methods
student.study('Math');
student.study('Science');
student.showSubjects();
}
Output:
Hi, I am Alice and I am 16 years old
Alice is walking
Alice is studying Math
Alice is studying Science
Alice studies: Math, Science
🔹 Method Overriding
Child classes can override parent methods to provide specific behavior:
class Shape {
String color;
Shape(this.color);
void draw() {
print('Drawing a $color shape');
}
double getArea() {
return 0.0;
}
}
class Circle extends Shape {
double radius;
Circle(String color, this.radius) : super(color);
@override
void draw() {
print('Drawing a $color circle with radius $radius');
}
@override
double getArea() {
return 3.14159 * radius * radius;
}
}
class Rectangle extends Shape {
double width;
double height;
Rectangle(String color, this.width, this.height) : super(color);
@override
void draw() {
print('Drawing a $color rectangle ${width}x$height');
}
@override
double getArea() {
return width * height;
}
}
void main() {
Shape shape = Shape('blue');
Circle circle = Circle('red', 5.0);
Rectangle rectangle = Rectangle('green', 4.0, 6.0);
shape.draw();
print('Area: ${shape.getArea()}');
circle.draw();
print('Area: ${circle.getArea().toStringAsFixed(2)}');
rectangle.draw();
print('Area: ${rectangle.getArea()}');
}
Output:
Drawing a blue shape
Area: 0.0
Drawing a red circle with radius 5.0
Area: 78.54
Drawing a green rectangle 4.0x6.0
Area: 24.0
🔹 Using Super Keyword
Access parent class methods and constructors using super:
class Employee {
String name;
double salary;
Employee(this.name, this.salary);
void work() {
print('$name is working');
}
void displayInfo() {
print('Employee: $name, Salary: \$${salary.toStringAsFixed(2)}');
}
}
class Manager extends Employee {
String department;
int teamSize;
Manager(String name, double salary, this.department, this.teamSize)
: super(name, salary);
@override
void work() {
super.work(); // Call parent method
print('$name is managing the $department department');
}
@override
void displayInfo() {
super.displayInfo(); // Call parent method
print('Department: $department, Team Size: $teamSize');
}
void conductMeeting() {
print('$name is conducting a team meeting');
}
}
class Developer extends Employee {
String programmingLanguage;
Developer(String name, double salary, this.programmingLanguage)
: super(name, salary);
@override
void work() {
super.work(); // Call parent method
print('$name is coding in $programmingLanguage');
}
void writeCode() {
print('$name is writing $programmingLanguage code');
}
}
void main() {
Manager manager = Manager('John', 8000.0, 'IT', 10);
Developer developer = Developer('Alice', 6000.0, 'Dart');
print('--- Manager ---');
manager.displayInfo();
manager.work();
manager.conductMeeting();
print('\n--- Developer ---');
developer.displayInfo();
developer.work();
developer.writeCode();
}
Output:
--- Manager ---
Employee: John, Salary: $8000.00
Department: IT, Team Size: 10
John is working
John is managing the IT department
John is conducting a team meeting
--- Developer ---
Employee: Alice, Salary: $6000.00
Alice is working
Alice is coding in Dart
Alice is writing Dart code