aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract class & constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abstract class & constructor?" Watch "Abstract class & constructor?" New topic
Author

Abstract class & constructor?

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

"An abstract class cannot be instantiated." Alright. Then why does it allows to write its constructor? Something thats eating my brains.. Please help.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What does the implementating subclass' constructor call?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

An abstract class can be extended by a concrete class. And when an instance of that class is created, the constructor of the abstract superclass is called as part of instantiating the subclass.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Ooooooh... so simple it is!
Thanks Barry, your way of making us all thinking is really excellent!
I conclude, inspite of having a constructor, an abstract class cannot be instantiated. Exceptional case!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Don't forget that an abstract class need not be completely full of abstract methods. The Java libraries are abound with "Adaptor" classes which are almost complete with default method implementations and and perhaps just one abstract method that has to be implemented by the subclass. These "Adaptor" superclasses have state that is set up by a constructor called by the subclass constructor.
[ March 03, 2007: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Thanks Barry, your way of making us all thinking is really excellent!


Thankyou Faisal, but basically I'm lazy
It's easier (sometimes) to get you guys to think than to write badly a whole lot of explanation that you can find better presented in an API or a book.
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Could you please give a real-world example of such sub-class-setting-adaptor-class's-state? Thanks in advance!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Here's a simple example:


For a more real-world one: see this from the StreamBase API. I leave it to you to provide a working implementation of the abstract class... it's a little beyond the scope of SCJP.
[ March 03, 2007: Message edited by: Barry Gaunt ]
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Many thanks for the code example Barry!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract class & constructor?