Inheritance and Access Modifiers in C#

Author - Webner
4.02.2020
|
0 Comments
||

In Inheritance, an object acquires all the properties and behaviours of its parent object automatically. It allows reusability of the code and we can even extend or modify the behaviours which are defined in other class.

Base and Derived Classes

We can derive a class from more than one class or interface and it can inherit data and functions from multiple base classes or interfaces.

Derived classes syntax −
<acess-specifier> class <base_class> {
...
}
class <derived_class> : <base_class> {
...
}

Advantage of Inheritance

Code reusability: We can reuse the members of the class any number of times. We do not need to declare them multiple times.

Types of Inheritance in C#

  1. Single Inheritance: Subclasses inherit the features of one superclass
    Class A{
    }
    Class B : A{
    }
  2. Multilevel Inheritance: A derived class will be inheriting a base class and the derived class also act as the base class to other classes.
    Class A{
    }
    Class B : A{
    }
    Class C : B{
    }
  3. Hierarchical Inheritance: One class serves as a superclass (base class) for more than one subclass.

    Class A{
    }
    Class B : A{
    }
    Class C : A{
    }
    Class D : A{
    }
  4. Multiple Inheritance(Through Interfaces): C# does not support multiple inheritance with classes. One class can have more than one superclass & inherit features from all parent classes. We can achieve multiple inheritance only through Interfaces.
    Interface A{
    }
    Interface B{
    }
    Class C : A,B{
    }

    C# inheritance example [Multilevel]

    using System;
    public class Birds{
    public void eat() { Console.WriteLine("Eating..."); }
    }
    public class Sparrow: Birds
    {
    public void chirp() { Console.WriteLine("Chirping..."); }
    }
    public class Crow : Sparrow
    {
    public void fly() { Console.WriteLine("Flying..."); }
    }
    class TestInheritance2{
    public static void Main(string[] args)
    {
    Crow d1 = new Crow ();
    d1.eat();
    d1.chirp();
    d1.fly();
    }
    }

Access Modifiers in C#

  1. Public: It has no limits which means any members or types defined as the public can be accessed within the class, assembly and even outside the assembly.
    class ClassTest
    {
    //Public method
    public void MethodPublic()
    {
    // defination of MethodPublic
    }
    }
    // to access the method
    class Program
    {
    static void Main(string[] args)
    {
    ClassTest objClassTest = new ClassTest();

    objClassTest.MethodPublic(); // valid code to access.
    }
    }

  2. Private: Limits the accessibility of a member within the defined type. For example, if a variable or a function is being created in Class A and declared as private, then another Class B can’t access that.
    using System;
    namespace Tutlane
    {
    class User
    {
    private string Name;
    private void GetUserDetails()
    {
    Console.WriteLine("Name: {0}", Name);
    }
    }
    class Program
    {
    static void Main(string[] args)
    {
    User u = new User();
    // Complier Error
    // These are inaccessible due to private specifier
    u.Name = "Suresh Dasari";
    u.GetUserDetails();
    Console.WriteLine("\nPress Enter Key to Exit..");
    Console.ReadLine();
    }
    }
    }
  3. Protected: Plays a role only when inheritance is used. Protected type or member becomes accessible only when a child is inherited by the parent.
    class ClassTest
    {
    //Protected variable
    protected int _abc;
    }

    class ClassTest2 : ClassTest
    {
    ClassTest2()
    {
    this._abc = 10; // can access from this class
    }
    }

    class ClassTest3
    {
    ClassTest3()
    {
    this._abc = 10; // can't access from this class
    }
    }

  4. Internal: Internal plays an important role when you want your class members to be accessible within the assembly. Hence, if you have a C# project that has Class A, Class B, and Class C, then any internal type and members will become accessible across the classes within the assembly.
    class ClassTest
    {
    internal void MethodInternal()
    {
    // do your code
    }
    }
    // to access the method
    class Program
    {
    static void Main(string[] args)
    {
    ClassTest objClassTest = new ClassTest();

    objClassTest.MethodInternal(); // valid code to access.
    }
    }

  5. Protected internal: It is a combination of protected and internal both. It will be accessible within the assembly due to its internal flavor and also via inheritance due to its protected flavor.
    class ClassTest
    {
    protected internal string name; // protected internal
    public void print()
    {
    Console.WriteLine("\nMy name is " + name);
    }
    }
    // to access the method
    class Program
    {
    static void Main(string[] args)
    {
    ClassTest objClassTest = new ClassTest();
    // Accepting value in protected internal variable
    objClassTest.name = "Arka";
    objClassTest.print();
    }
    }
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *