aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract constructor question in rules roundup game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract constructor question in rules roundup game" Watch "abstract constructor question in rules roundup game" New topic
Author

abstract constructor question in rules roundup game

Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

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.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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?!


OCPJP 6
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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

Joined: Aug 06, 2010
Posts: 173
Nice one Dieter !!!


Regards,
Avishkar Nikale
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: abstract constructor question in rules roundup game