0% found this document useful (0 votes)
12 views5 pages

Untitled Document

Inheritance is a process where a child class inherits features from a parent class, allowing for code reuse and reducing redundancy. The 'super' keyword is used in the child class to access properties, constructors, and methods of the parent class. Benefits of inheritance include code reuse, clean code, easy maintenance, and scalability for future child classes.

Uploaded by

khanbabax003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Untitled Document

Inheritance is a process where a child class inherits features from a parent class, allowing for code reuse and reducing redundancy. The 'super' keyword is used in the child class to access properties, constructors, and methods of the parent class. Benefits of inheritance include code reuse, clean code, easy maintenance, and scalability for future child classes.

Uploaded by

khanbabax003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Inheritance:

Inheritance eik process hai jisme eik (child class) dose class(parent class) ke features
jise properties/constuctor/Method KO inherit karthi [Link] code reuse hotha Hain
our bar-bar same cheez likhne ki zarorat Nahi padti.

Simple Definition:
Eik (child class) mah (parent class) keh feature KO apne ander free meh lah Sakti hai.

Extands;
Es kah kaam hotha hai parent Class keh sare features child class me lah ahtha hain.

super keyword;
child class ke andar parent class ke Properties/Constructor/Methods KO access Karne
Kaliyah super use kiyah jahtha hai.

●​ [Link]()
parent class keh variables/properties access kartha hain
●​ super()
parent class keh constructor KO call kartha hai.
●​ [Link]()
parent class keh method KO call kartha hai.
Example without inheritance(problem)

class Person {
var name;
var age;

void display() {
print('Name: $name, Age: $age');
}
}

class Student extends Person {


var rollNo;

void showStudent() {
print('Roll No: $rollNo');
}
}

void main() {
var student = Student();
[Link] = 'Ahmed';
[Link] = 20;
[Link] = 101;

[Link]();
[Link]();
}


Problem:
Teacher aur Student dono me same properties (name, age) repeat ho rahi hain.

Code long aur duplicate ban gaya.


Solution With Inheritance

// Parent Class
class Person {
var name;
var age;

void display() {
print('Name: $name, Age: $age');
}
}

// Child Class
class Teacher extends Person {
var subject;

void showTeacher() {
print('Teacher of $subject');
}
}

// Another Child Class


class Student extends Person {
var rollNo;

void showStudent() {
print('Student Roll No: $rollNo');
}
}

void main() {
var teacher = Teacher();
[Link] = 'Ali';
[Link] = 30;
[Link] = 'Math';
[Link](); // From Parent
[Link](); // From Child

print('-----');

var student = Student();


[Link] = 'Ahmed';
[Link] = 20;
[Link] = 101;
[Link](); // From Parent
[Link](); // From Child
}
Output:

Name: Ali, Age: 30


Teacher of Math
-----
Name: Ahmed, Age: 20
Student Roll No: 101

---

How it Works:

Person = Parent (Base) Class

Teacher aur Student = Child Classes

extends Person ka matlab hai:

> "Teacher class automatically Person class void aur methods inherit kar rahi hai."

---

Benefits of Inheritance:

Benefit Explanation:

●​ Code Reuse
Parent ke code ko child directly use kar sakta hai.
●​ Clean Code
Duplicate code hat jata hai.
●​ Easy Maintenance
Agar parent me change karein to wo sab child classes me apply ho jata hai.
●​ Scalability
Future me naye child add karna easy.
Inheritance (extends)

class Parent {
// properties
var name;

// constructor
Parent([Link]);

// method
void display() {
print('Parent Name: $name');
}
}

// Child class extends Parent


class Child extends Parent {
var age;

// constructor
Child(String name, [Link]) : super(name);

// method
void show() {
print('Child Age: $age');
}
}

void main() {
// object create + constructor call
var obj = Child('Ali', 20);
// call parent class method
[Link]();
// call child class method
[Link]();
}

//Output
Parent Name: Ali
Child Age: 20

You might also like