File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract classes??

 
srikanth reddy
Ranch Hand
Posts: 252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,

i have a doubt ....
as u know that abstract classes cannot be instantiated .....right !!!

then in KnB it says every class including abstract class should have

constructors.....so whats the use of having of constructors when we are

not able to instantiate the class...might be iam missing

somewhere...someone please clarify ......
 
Ramesh Mangam
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srikanth,

When you extend a abstract class and instantiate an object of subclass, it not only calls its own constructor but also all the superclass constructors in the inheritance hierarchy up to java.lang.Object. There might be some other reasons too. Can someone throw some light on this?
 
Joan Pujol
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should have? Or can have? Or have?

I think that should have is not correct, it is?
But abstract classes can have constructors and have constructors (the default one)
 
Akshay Kiran
Ranch Hand
Posts: 220
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Things get difficult when we don't have the exact context.

when somebody asks:
"does he have a credit card?"
and is answered "he should have one"
isn't the same as saying "he should have one" without the context. It just means "he probably does"

in any case, if the question is "should we provide a constructor to abstract classes", the answer is, "it depends on your needs", but no, its not necessary all the time. the compiler provides a default constructor to handle subclassing problems.

but if the thing was said in a "do's and don'ts" list,
then YES, every class should have a constructor to make it more readable and improve clarity.
 
Ray Horn
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the abstract class implements a subset of behavior (some methods) you may need to establish the state on a new instance (ex: set private instance variables) and doing this in the constructor is the way to go.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic