What is Inheritance?

1/9/2020 6:43:22 AM

Inheritance is very important for C# and other object-oriented programming. It’s parent and child relation between two or more class. When a class inherit from another class then the all parents class property will available to the child class. Also, the child class contain the itself property. Some time we called Super Class and Sub Class. Inheritance concept create code reusability. When we inherit then we can’t access private property. Inheritance symbol is in C# “ : ”. Now given bellow the example of the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObjectTest
    internal class Students
        public int ID { get; set; }
        public string Name { get; set; }
        private int AccountId { get; set; }
        public string Email { get; set; }
    internal class Human: Students
        internal string Password { get; set; }
    class Program
        static void Main(string[] args)
            //Create an object and initialize the constructor by the new keyword.
            Human obj = new Human();
            obj.Name = "Farhan Sakib Jesy";
            obj.Email = "";
            obj.Password = "12345";
            Console.WriteLine("Name : "+ obj.Name);
            Console.WriteLine("Email : " + obj.Email);
            Console.WriteLine("Password : " + obj.Password);

In this code we will see student is parent class and human are child class. This human class Inherit to the student class using “:” symbol. We can access the student class property by the human class object.