IN AND OUT OF CONSTRUCTORS
1. What is the necessity of parameterised
constructor?
Ans: When we try to create object for a class below syntax is
used
Class obj = new Class();
When above code runs object of the class is created by calling the
default constructor of the class.
If we need to give the data to the properties of the object at the time
Of creation itself just create parameterised constructor.
Example:
public class Account
{
public string Name { get; set; }
public long Mobile { get; set; }
public string City { get; set; }
public Account()
{
Console.WriteLine("Base class constructor");
}
public Account(string Name, long Mobile, string City)
{
this.Name = Name;
this.Mobile = Mobile;
this.City = City;
Console.WriteLine("Parameterised constructor");
}
}
static void Main(string[] args)
{
//Parameterised constructor for Abstract Base
class
Console.WriteLine("Create empty object");
Account obj = new Account();
Console.WriteLine("Name:{0}", obj.Name);
Console.WriteLine("MObile:{0}", obj.Mobile);
Console.WriteLine("City:{0}", obj.City);
Console.WriteLine("Create initialised object");
Account iniObj = new Account("Suri", 8686123456, "Hyd");
Console.WriteLine("Name:{0}", iniObj.Name);
Console.WriteLine("Mobile:{0}", iniObj.Mobile);
Console.WriteLine("City:{0}", iniObj.City);
Console.ReadLine();
}
Output:
2.
What’s the necessity to call base class
constructor from derived class constructor?
As discussed earlier
parameterised constructor is to create initialised object. If parameterised
constructor is not there default constructor is called.
Coming to this question there is
no rule that if derived class has parameterised constructor Base class should
have parameterised constructor. Just by passing all the parameters to the
derived class constructor we can create initialised object for derived class.
But if have multilevel
inheritance it won’t be comfortable or good initialise all the properties
at the derived class.
When object is created for the
derived class, base class constructor is called first and then base class
constructor is called.
So it’s good to create
parameterised constructors for every base class.so that we won’t miss
initialising any property.
Example:
Base class.
public class Account
{
public string Name { get; set; }
public long Mobile { get; set; }
public string City { get; set; }
}
Derived Class
public class CurrentAccount : Account
{
public int AccountNumber { get; set; }
public string AccountType { get; set; }
public CurrentAccount(int acctNumber,string acctType,string Name,
long Mobile,string City)
{
this.AccountNumber = acctNumber;
this.AccountType = acctType;
this.Mobile = Mobile;
this.City = City;
this.Name = Name;
Console.WriteLine("Parameterised
Derived class Current Account");
}
}
Derived class can initialise base class
properties. Hence it’s not mandatory to call base class constructor from
derived class.
But we are most likely to miss value to
any property if all initialised in derived class.
So below method is followed.
Example:
public class Account
{
public string Name { get; set; }
public long Mobile { get; set; }
public string City { get; set; }
public Account()
{
Console.WriteLine("Base class constructor");
}
public Account(string Name, long Mobile, string City)
{
this.Name = Name;
this.Mobile = Mobile;
this.City = City;
Console.WriteLine("Parameterised Base class
constructor");
}
}
public class CurrentAccount : Account
{
public int AccountNumber { get; set; }
public string AccountType { get; set; }
public CurrentAccount(int acctNumber,string acctType,string Name,long Mobile,string City):base(Name,Mobile,City)
{
this.AccountNumber = acctNumber;
this.AccountType = acctType;
Console.WriteLine("Parameterised Derived class
Current Account");
}
}
In Program.cs
static void Main(string[] args)
{
//Parameterised constructor for Abstract Base
class
CurrentAccount iniObj = new CurrentAccount(1234678,"current","Suri",8686123456,"Hyd");
Console.WriteLine("Name:{0}", iniObj.Name);
Console.WriteLine("Mobile:{0}", iniObj.Mobile);
Console.WriteLine("City:{0}", iniObj.City);
Console.WriteLine("Acount Number:{0}",
iniObj.AccountNumber);
Console.WriteLine("account Type:{0}", iniObj.AccountType);
Console.ReadLine();
}
Output:
3. Which constructor will be called first if
have base class constructor and derived class constructor?
As given in the previous example Base class
constructor is called first.Then the derived class constructor will be called.
4. what if we have parameterised constructor
for derived class and don't parameterised constructor for base class?
As explained before it’s not mandatory to
have paramterised constructor for base class if we have for derived class.
5. Why do Abstract class have constructor?
Even though Abstract class is something for
which we cannot create object we need parameterised constructor.
For the comfort of writing code for
assigning values for properties along with class we need
parameterised constructor.
Even though Abstract class can not have
object ,the class which is going to implement it’s method will have object.So
atleast for that derived class object values are to be set for inherited
properties from base class.
That’s why C# allows us create
parameterised constructor.
For testing make the above Account method
as Abstract Method.
6. Can we create constructor for Static class?
No Static class can not have
constructor.Static class memebers are never meant for getting initialised..
There will never be a situation to write
constructor and the same is not allowed.
7. What is a private constructor? When it gets
class.
p If a constructor is declared as private,class can never be instantiated.
8. What is Static Constructor? Why is it
needed and when is it called?
Static constructor is used to assign value
for static properties.
Derived class static constructor is called
First.
Then Base class static constructor is
called.
Then Base class constructor is called.
Then Derived class constructor is called.
Static class constructor won’t have access
specifiers and parameters.
Example:
public class CurrentAccount : Account
{
public int AccountNumber { get; set; }
public string AccountType { get; set; }
public static double InterestRate { get; set; }
static CurrentAccount()
{
InterestRate = 10.5;
Console.WriteLine("Static Constructor Derived
class Current Account");
}
public CurrentAccount(int acctNumber,string acctType,string Name,long Mobile,string City):base(Name,Mobile,City)
{
this.AccountNumber = acctNumber;
this.AccountType = acctType;
Console.WriteLine("Parameterised Derived class
Current Account");
}
}
public class Account
{
public string Name { get; set; }
public long Mobile { get; set; }
public string City { get; set; }
public static int Tenure { get; set; }
public Account()
{
Console.WriteLine("Base class constructor");
}
static Account()
{
Tenure = 3;
Console.WriteLine("Static Base class constructor
");
}
public Account(string Name, long Mobile, string City)
{
this.Name = Name;
this.Mobile = Mobile;
this.City = City;
Console.WriteLine("Parameterised Base class
constructor");
}
static void Main(string[] args)
{
//Parameterised constructor for Abstract Base
class
CurrentAccount iniObj = new CurrentAccount(1234678,"current","Suri",8686123456,"Hyd");
Console.WriteLine("Name:{0}", iniObj.Name);
Console.WriteLine("Mobile:{0}", iniObj.Mobile);
Console.WriteLine("City:{0}", iniObj.City);
Console.WriteLine("Acount Number:{0}",
iniObj.AccountNumber);
Console.WriteLine("account Type:{0}", iniObj.AccountType);
Console.ReadLine();
}
9. What is constructor Chain?
Suppose we have multiple constructors for
the same class, to restrict the data that is assigned for the properties, if we
need to put some condition before assigning the values properties, it becomes
redundant writing the same code for every constructor.
So instead of writing initialising logic in
every constructor we add in only one constructor.
And same constructor is called from every
constructor.
No comments:
Post a Comment