-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathInheritance.cs
78 lines (69 loc) · 2.46 KB
/
Inheritance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Inheritance is a mechanism in which one class acquires the properties and behavior of another class.
* It represents the IS-A relationship which is also known as a parent-child relationship.
* single level: only one parent A->B
* multi level: chain of parents A->B->C
*
* multiple: one child can have two parents -> not possible due to class -> only possible using interface, check InterfaceExpression.cs
*/
using System;
namespace Basics
{
//parent class
class Employee
{
public int id;
public string name;
public string address;
public string department;
public void SetEmployee(int id, string name, string address, string department)
{
this.id = id;
this.name = name;
this.address = address;
this.department = department;
}
public void GetEmployee()
{
Console.WriteLine($"Id: {this.id}");
Console.WriteLine($"Name: {this.name}");
Console.WriteLine($"Address: {this.address}");
Console.WriteLine($"Department: {this.department}");
}
}
// teacher is a sub-class of employee(which is parent)
// single level inheritance
class Teacher : Employee
{
protected string subject;
protected string qualification;
public void SetTeacher(string subject, string qualification, int id, string name, string address, string department)
{
SetEmployee(id, name, address, department);
this.subject = subject;
this.qualification = qualification;
}
public void GetTeacher()
{
GetEmployee();
Console.WriteLine($"Subject: {this.subject}");
Console.WriteLine($"Qualification: {this.qualification}");
}
}
// part time teacher is a sub-class of teacher(which is parent) and teacher is a sub-class of employee(which is parent)
// so, multilevel inheritance is A->B->C
class PartTimeTeacher : Teacher
{
private int hours;
public void SetPartTimeTeacher(int hours, string subject, string qualification, int id, string name, string address, string department)
{
SetTeacher(subject, qualification, id, name, address, department);
this.hours = hours;
}
public void GetPartTimeTeacher()
{
GetTeacher();
Console.WriteLine($"Hours: {this.hours}");
}
}
}