Interview Questions on .Net Part: 1



Abstraction

Abstraction means displaying or allows access to that part of program or class which is important. Take an example.

How to achieve that?

Take the below example in which I am declaring four string and out of them only one is public and rest are private or protected. When we create object of this class and try to access all these variables then we get permission to access only public variable.


class AbstractionWaliClass
    {
        private string my_id;
        private string my_name;
        public string my_contact_no;
        protected string protected_id;
    }
 

Abstract Class:

If you want to create a class that is only base class.

No one can make instance of it. Then you should use Abstract class with the help of keyword ‘abstract’.

abstract class AbstractClass
    {

    }

Now what inside this class?

Any method doesn’t necessary it should be abstract method but it can also contains non abstract method.


public abstract class AbstractClass

    {
        public int my_first_method()
        {
            return 1;
        }
        public abstract void second_class();
    }
  


Now in main method code like this:

class Program:AbstractClass
    {
        static void Main(string[] args)
        {
            Program obj = new Program();
            Console.WriteLine(obj.my_first_method());
            obj.second_class();
            Console.ReadLine();
          
           
        }
//Here we are declaring the abstract method named secomd_class and writing code by overriding the abstract method
        public override void second_class()
        {
            Console.WriteLine("this is abstract method");
        }
      
    }

 
Know more about abstract class:

1. It cannot be sealed because sealed class is restricted to be inherited.

2. While overriding the abstract method, method type should be same as  you can see here that second_class() method is void in both condition.

3. Abstract method should not be private since it will be gain in accessible due to private keyword.

4. Abstract method is by default public.

5. Constructor and destructor can also be in abstract class but they should be private or protected but not public.

6. You will have to implement all abstract method in your class if you are deriving it from abstract class. Otherwise it will show compile time error.  


Comments

Popular posts from this blog

DTS package conversion in SSIS to use in SQL 2012 and SQL 2016

Infinite Scroll in Asp.Net C# using Javascript without Third party Plugin

Add Custom Tags in Web config file in ASP .Net