As we know we can create a constructor in abstract class, whts the use of the writeing the constructor in abstract class, as we are not able to create a object of the abstract class, where actually microsoft using this facility.... in c#, pls clerify my
dought...........
in additon to mbischoff in abstract class not only each method are abstract some method can be concrete there (implemented), so to do the require functionality you can use the constructor for some intialization like things.
thanks
vishal
Marked as answer by Kevin Yu - MSFT on Jun 07, 2007 06:23 AM
i have a question.. if the class which derives from the abstract class also has the constructor( of its own) then, will the abstract classe's constuctor be invoked??? as per my knowledge the parent constructor will be invoked only if the child doesnt have
one.correct me if i'm wrong... :)
thanks
Mythu
---------------------------------------------
Please Mark Post that helped you as answer
the constructor of the base class is always called before the constructor of the derived class. E.g. in the following example the comment indicates the order in which the statements are executed:
namespace ConsoleApplication1
{
public abstract class Parent
{
DateTime d = DateTime.Now; // 2
public Parent()
{
string a = "hello"; // 3
}
}
public class Child : Parent
{
public Child()
{
string b = "asdf"; // 4
}
}
class Program
{
static void Main(string[] args)
{
Child c = new Child(); // 1
}
}
}
Regards,
Martin
Please mark this post as Answer if it was helpful.
constructor is a default method in each class you need not to define it explicitly, we only use constructor when we have some requirement accordingly , otherwise once you define a class constructor will be by default
just remind how we create object of a class in C#
MyClass identifier = new MyClass(); --- this is the constructor method.
so not only the class constructor each and every base class constructor will get called.
rprashanthku...
Member
2 Points
87 Posts
As we know we can create a constructor in abstract class
Jun 04, 2007 11:12 AM|LINK
As we know we can create a constructor in abstract class, whts the use of the writeing the constructor in abstract class, as we are not able to create a object of the abstract class, where actually microsoft using this facility.... in c#, pls clerify my dought...........
waiting for ur nice reply
mbischoff
Participant
852 Points
127 Posts
Re: As we know we can create a constructor in abstract class
Jun 04, 2007 12:41 PM|LINK
The constructor of the abstract class gets called when you create an instance of a class which derives from the abstract class.
Regards,
Martin
-- Martin
vishalsharma...
Member
364 Points
107 Posts
Re: As we know we can create a constructor in abstract class
Jun 05, 2007 07:30 PM|LINK
in additon to mbischoff in abstract class not only each method are abstract some method can be concrete there (implemented), so to do the require functionality you can use the constructor for some intialization like things.
thanks
vishal
mythu
Member
136 Points
54 Posts
Re: As we know we can create a constructor in abstract class
Jun 06, 2007 10:36 AM|LINK
i have a question.. if the class which derives from the abstract class also has the constructor( of its own) then, will the abstract classe's constuctor be invoked??? as per my knowledge the parent constructor will be invoked only if the child doesnt have one.correct me if i'm wrong... :)
thanks
Mythu
Please Mark Post that helped you as answer
mbischoff
Participant
852 Points
127 Posts
Re: As we know we can create a constructor in abstract class
Jun 06, 2007 02:33 PM|LINK
Hi Mythu,
the constructor of the base class is always called before the constructor of the derived class. E.g. in the following example the comment indicates the order in which the statements are executed:
namespace ConsoleApplication1 { public abstract class Parent { DateTime d = DateTime.Now; // 2 public Parent() { string a = "hello"; // 3 } } public class Child : Parent { public Child() { string b = "asdf"; // 4 } } class Program { static void Main(string[] args) { Child c = new Child(); // 1 } } }Regards,
Martin
-- Martin
vishalsharma...
Member
364 Points
107 Posts
Re: As we know we can create a constructor in abstract class
Jun 06, 2007 02:36 PM|LINK
hi
constructor is a default method in each class you need not to define it explicitly, we only use constructor when we have some requirement accordingly , otherwise once you define a class constructor will be by default
just remind how we create object of a class in C#
MyClass identifier = new MyClass(); --- this is the constructor method.
so not only the class constructor each and every base class constructor will get called.
copy paste the following code
public abstract class BaseClass1{
public BaseClass1 (){
string str = "hello";}
public string FirstName;}
public class Class1 : BaseClass1{
public Class1 (){
// // TODO: Add constructor logic here //}
}
i hope it will makes you something more clear.
thanks
vishal
mythu
Member
136 Points
54 Posts
Re: As we know we can create a constructor in abstract class
Jun 12, 2007 07:29 AM|LINK
Thans Martin and vishal
Please Mark Post that helped you as answer