User Name:


User Email:




This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2018 - First Crazy Developer (Abhishek Kumar)
  

crazydeveloper Can an abstract class have a constructor?

Can an abstract class have a constructor?

Hi guys, today we will discuss some unclear concept of abstract class. Tell me one thing that, can an abstract class have a constructor? If your answer is yes then what is the use?

Most of times we heard what is an abstract class, use of an abstract class and difference between abstract class and interface? Today we will move forward and discuss a new topic, constructor in an abstract class and their use.

First I give you answer what I asked, yes an abstract class can have a constructor. Confused?

Just like in other class we use constructor to initialise fields & properties same we can do with abstract class with constructor. Means an abstract class constructor is used to initialise field and properties of the abstract class.

Next question comes in our mind we cannot create the object of an abstract class. So, when and why we need that?

We would provide a constructor for an abstract class if we need to initialise any fields and properties of the abstract class before the instance of a child class initialised.

An abstract class constructor can also be used to execute code that is relevant  for all child class. This is prevent repeated code in our programs.

We knew that we can not create an object of an abstract class but important  part is that we can create an object of child class which are derived from the abstract class, now the scope of abstract class constructor comes. When the child class object is created, the parent abstract class constructor is automatically called through child class.

To implement the above discussion we go through a real example with the code. There is the scenario that power company have different types of customer. For example corporate customer and domestic customer. Now each and every customer has some unique identification number. So how can we design and implement these  requirements in our program.

abstract_constructor

First Step :-

We defined an abstract class of customer, Which has a property ID.
 

 1: public abstract class Customer
 2:     {
 3:         private Guid _id;
 4:         protected Guid ID
 5:         {
 6:             get
 7:             {
 8:                 return this._id;
 9:             }
 10:         }        


       
In above code an abstract class, which has private field _id and protect property ID. Now this class inherited by multiple types of customer class and  every customer has their unique ID. We will have write this code in every child  class. But business requirements tell us that whenever customer object is created that should be with unique Id. How we fulfil these requirements? Now I modified the abstract class.

 

 1: public abstract class Customer
 2:     {
 3:         private Guid _id;
 4:         protected Guid ID
 5:         {
 6:             get
 7:             {
 8:                 return this._id;
 9:             }
 10:         }
 11:         protected Customer()
 12:         {
 13:             this._id = new Guid();
 14:         }
 15:        
 16:     }


Now I added a constructor with an abstract class and assigned a new unique Id with their field. Now look the following code:
 

 1: public class CorporateCustomer :Customer
 2:     {
 3:  
 4:     }
 5:     public class DomesticCustomer : Customer
 6:     {
 7:  
 8:     }



In above code we defined two class which inherited from Customer class.

 1: public class RegisterCustomer
 2:     {
 3:         CorporateCustomer cc = new CorporateCustomer();
 4:         DomesticCustomer dc = new DomesticCustomer();
 5:  
 6:         public void print()
 7:         {
 8:            var ccId=cc.ID;
 9:            var dcId = dc.ID;
 10:         }
 11:  
 12:     }



In above when we create the object of CorporateCustomer and DomesticCustomer, new unique id created before object intialization. So we can say that the constructor of the parent abstract class execute before the child class construtor. Now we also removed the repeated code in above code to generate new  unique Id in common place. Good practice to use protected access modifier with  abstract class constructor. because using the public does not make sense here.






crazydeveloper Home Page 27 June 2015

Become a Fan