Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can abstract class have constructor

 
Hemal Mehta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If so why?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure they can...
you cannot instantiate an abstract class but when you subclass it and you create an instance of your concrete subclass you may want to initialize things in your abstract superclass. This is why constructors in abstract classes may be useful...
Remember that when you create an instance of an object, all classes in the hierarchy tree have to be initialized too and their constructors will be invoked. So even if the class is abstract his constructor(s) will be invoked...
[ February 26, 2002: Message edited by: Valentin Crettaz ]
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare a class as a abstract even if it does not have a single abstract method, i.e. you can declare a fully implemented class as abstract.
Brian
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is some code that shows what Valetin said

[ February 26, 2002: Message edited by: Rajinder Yadav ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rajinder,
I was gonna write some code but I had to take off, so you spared me some work... How kind of you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic