-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinheritance.cs
55 lines (47 loc) · 1.35 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
using System;
namespace InheritanceDemo
{
// this is our Base/Parent class
public class Person
{
// Base/Parent class properties
public string Name { get; set; }
public int Age { get; set; }
// Base/Parent class method
public void DisplayPersonInfo()
{
Console.WriteLine($"Name: {Name}, Age: {Age}");
}
}
// Implementing Inheritance for Student Class
public class Student : Person
{
// Derived/Child class Property
public string RollNumber { get; set; }
// Derived/Child class method
public void DisplayStudentInfo()
{
Console.WriteLine($"Student Roll Number: {RollNumber}");
}
}
class Program
{
static void Main()
{
// Creating an object of the derived/child class
// through Object-initializer technique
// accessing properties declared in both
// Parent and Child classes
Student Asfar = new()
{
Name = "Asfar Hussain",
Age = 12,
RollNumber = "IT-2013"
};
// Accessing base/parent class method
Asfar.DisplayPersonInfo();
// Accessing derived/child class method
Asfar.DisplayStudentInfo();
}
}
}