Wednesday 12 July 2017

Abstract Class level Interview Questions - C#

Can an abstract class have a constructor? If so what is the use?
Yes, an abstract class can have a constructor. In general, a class constructor is used to initialize fields. Along the same lines, an abstract class constructor is used to initialise fields of the abstract class. You would provide a constructor for an abstract class if you want to initialise certain fields of the abstract class before the instantiation of a child-class takes place. An abstract class constructor can also be used to execute code that is relevant for every childclass. This prevents duplicate code.

You cannot create an instance of an abstract class. So, what is the use of a constructor in an abstract class?
Though you cannot create an instance of an abstract class, we can create instances of the classes that are derived from the abstract class. So, when an instance of derived class is created, the parent abstract class constructor is automatically called.

Note: Abstract classes can't be directly instantiated. The abstract class constructor gets executed thru a derived class. So, it is a good practice to use protected access modifier with abstract class constructor. Using public doesn't make sense.

Can you call an abstract method from an abstract class constructor? If so, what is the use of it?
Yes, an abstract method can be called from an abstract class constructor.  Click here

An abstract method in an abstract class does not have any implementation, so what is the use of calling it from the abstract class constructor? 

If you want the abstract method to be invoked automatically whenever an instance of the class that is derived from the abstract class is created, then we would call it in the constructor of the abstract class.

Output Related Programs.

public abstract class absClass
{
    public abstract string sayhello();
}

public class childClass : absClass
{
    public override string sayhello()
    {
        return "hello";
    }
}
public class childClass2
{
    public string getName()
    { return "suraj"; }
}

public class Program2
    {
        static void Main()
        {         
            absClass absObj = new absClass(); 
// Cannot create an instance of the abstract class or interface 'absClass'
            absClass absObj2 = new childClass();
            absClass absObj3 = new childClass2();
//Cannot implicitly convert type 'testDemo.childClass2' to 'testDemo.absClass'
            childClass obj1 = new childClass();
            childClass obj2 = new absClass(); 
//Cannot create an instance of the abstract class or interface 'absClass'
            childClass obj3 = new childClass2(); 
//Cannot implicitly convert type 'testDemo.childClass2' to 'testDemo.childClass'
            childClass2 cobj1 = new childClass2();
            childClass2 cobj2 = new childClass();
//Cannot implicitly convert Cannot implicitly convert type 'testDemo.childClass' to 
'testDemo.childClass2'
            
            childClass2 cobj3 = new absClass(); 
//Cannot create an instance of the abstract class or interface 'absClass'
            
            Console.Read();
        }  
    }


0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives