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

Abstract class & constructor?

 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the implementating subclass' constructor call?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please give a real-world example of such sub-class-setting-adaptor-class's-state? Thanks in advance!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks for the code example Barry!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic