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.
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