Abstract class Core Concepts in C#.Net


1)What is the use of abstract class?

Abstract class mainly used for reusability purpose.

Abstract class is intended only to be a base class of other classes.

Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.

Abstract class contain some of implemented methods, we can use this methods or we can override this method defination in derived class.

2)What is the need of define constructor in abstract class?

To intialize the veariables of class we define constructor in abstract class.

When we create object to the derived class its implicitly calls the base class constructor

 public abstract class MyAbsClass
   {
       public MyAbsClass()
       {
           Console.WriteLine("I am from myABsClass");
       }
   }
   public class MyDerivedClass : MyAbsClass
   {
       public void dispaly()
       {
           Console.WriteLine("I am from MyDerivedClass");
       }
   }
    class Program
    {
        static void Main(string[] args)
        {
            MyDerivedClass obj = new MyDerivedClass();
            obj.dispaly();
            Console.ReadLine();
        }
    }
o/p:
I am from myABsClass
I am from MyDerivedClass

3)Can we define class as private ?

No, we can not define class as private , protected or protected internal.


4)Can we define interface as protected?

No, we can not define interface as private , protected or protected internal.

5)Is it mandatory to implement abstract methods in c#?

yes, you should implement all the abstrct methods in abstract class or else it showing error.

if you have more than one method and you need only one then you can split your class into multiple abstrct class according SRP(single responsibilty principle).

6)Is it mandatory to implement all defined methods of abstract class in c#?

Not necessory.

7)Do we private methods in abstract class?

yes we have private methods. which is called from public methods.

 public abstract class MyAbsClass
   {
       public MyAbsClass()
       {
           Console.WriteLine("I am from myABsClass");
       }
       public void getData()
       {
           privatemethod();
       }
       private void privatemethod()
       {
           Console.WriteLine(" I am from private method");
       }
       public abstract void putdata();
   }
   public class MyDerivedClass : MyAbsClass
   {
       public override void putdata()
       {
           Console.WriteLine("I am from implemented abstrct method");
       }

   }
   class Program
    {
        static void Main(string[] args)
        {
            MyDerivedClass obj = new MyDerivedClass();
            obj.getData();
     
            Console.ReadLine();
        }
    }

o/p:

I am from myABsClass
I am from private method

8)Is this possible to declare abstract method as a static?

No, you can not define abstract member as private,static.

9)we have static methods in abstract class?

yes , we can we have static methods.


Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.

No comments:

Post a Comment