oopWithDart
class Person { String name; int age; void introduce() { print('My name is $name, and I am $age years old.'); } } void main() { var person = Person(); person.name = 'Alice'; person.age = 30; person.introduce(); }class Person { String name; int age; // Parameterized constructor Person(this.name, this.age); void introduce() { print('My name is $name, and I am $age years old.'); } } void main() { var person = Person('Alice', 30); person.introduce(); }class Student extends Person { String school; Student(String name, int age, this.school) : super(name, age); void study() { print('$name is studying at $school.'); } } void main() { var student = Student('Bob', 20, 'ABC School'); student.introduce(); // Inherited from Person class student.study(); }class Shape { void draw() { print('Drawing a shape.'); } } class Circle extends Shape { @override void draw() { print('Drawing a circle.'); } } void main() { Shape shape = Circle(); shape.draw(); // Calls Circle's draw method }class BankAccount { double _balance = 0.0; // Private field void deposit(double amount) { if (amount > 0) { _balance += amount; } } double getBalance() { return _balance; } }abstract class Shape { void draw(); // Abstract method } class Circle extends Shape { @override void draw() { print('Drawing a circle.'); } }
Last updated