Tuesday, January 25, 2022

Inheritance in C#

 In c#, Inheritance is one of the primary concepts of object-oriented programming (OOP), and it is used to inherit the properties from one class (base) to another (child) class.

In c# inheritance, the class whose members are inherited is called a base (parentclass, and the class that inherits the members of the base (parent) class is called a derived (childclass.


Following is the syntax of implementing an inheritance

<access_modifier> class <base_class_name>
{
// Base class Implementation
}

<access_modifier> class <derived_class_name> : <base_class_name>
{
// Derived class implementation
}

Example:-

public class x
{
    public void GetDetails()
    {
       // Method implementation
    }
}
public class Y: X
{
    // your class implementation
}
class Program
{
   static void Main(string[] args)
   {
       Y y = new Y();
       y.GetDetails();
   }
}

If you observe the above example, we defined a class “X” with the method called “GetDetails” and the class “Y” is inheriting from class “X”. After that, we call a “GetDetails” method by using an instance of derived class “Y”.

Example 2:-

using System;

namespace ArjunInheritanceExample
{
    public class User
    {
       public string Name;
       private string Location;
       public User()
       {
          Console.WriteLine("Base Class Constructor");
       }
       public void GetUserInfo(string loc)
       {
          Location = loc;
          Console.WriteLine("Name: {0}", Name);
          Console.WriteLine("Location: {0}", Location);
       }
    }
    public class Details: User
    {
       public int Age;
       public Details()
       {
          Console.WriteLine("Child Class Constructor");
       }
       public void GetAge()
       {
          Console.WriteLine("Age: {0}", Age);
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
          Details d = new Details();
          d.Name = "Arjun Walmiki";
          // Compile Time Error
          //d.Location = "Thane";
          d.Age = 35;
          d.GetUserInfo("Thane");
          d.GetAge();
          Console.WriteLine("\nPress Any Key to Exit..");
          Console.ReadLine();
       }
    }
}

If you observe the above example, we defined a base class called “User” and inheriting all the user class properties into a derived class called “Details” and we are accessing all the members of the User class with an instance of the Details class.

 

If we uncomment the commented code, we will get a compile-time error because the Location property in the User class is defined with a private access modifier. The private members can be accessed only within the class.

 

When you execute the above c# program, you will get the result as shown below.


Base Class Constructor

Child Class Constructor

Name : Arjun Walmiki

Location : Thane

Age : 35

Press Any Key to Exit..


If you observe the above result, we are able to access all the properties of base class into child class based on our requirements.

No comments:

Post a Comment