File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract constructor question in rules roundup game

 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the JavaRanch Rules Roundup game, there's a question that says basically "Can a constructor be declared abstract?" The answer is No, because "a constructor is used to instantiate a class, and an abstract class can never be instantiated, so it would make no sense."

But an abstract class CAN have constructors... Just not abstract ones. I guess an Abstract class could benefit from a constructor when its subclass is instantiated, for the sake of inheritance, and a constructor could not be inherited, because that is native to a constructor, so all in all it doesn't make sense to have an abstract constructor.

But if you should say an abstract constructor could happen, obviously where you went wrong is not that you forgot that a constructor is used to instantiate a class, but that you:

a) Forgot that constructors cannot be inherited
b) Got confused as to the real purpose of the question (whether abstract classes can have constructors)

Therefore, I think the explanation is not very accurate.
Also, I think I had better get some sleep.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dieter Quickfend wrote:
But an abstract class CAN have constructors... Just not abstract ones. I guess an Abstract class could benefit from a constructor when its subclass is instantiated, for the sake of inheritance, and a constructor could not be inherited, because that is native to a constructor, so all in all it doesn't make sense to have an abstract constructor.

An abstract class's constructor would come into play when you instantiate its subclass. Remember that rule - call to super() must be first statement in constructor.
Your subclass will automatically call your abstract class's empty constructor - doesnt matter if or if not you have one, unless you specify explicitly to call some other overloaded constructor.
So, I guess...it makes some sense, doesn't it - to have a constructor for an abstract class?!
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops..did you say the same thing?!
So, this post is just for explaining that question, right? My bad, I mis-interpreted.
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice one Dieter !!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic