File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract classes?? 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 classes??" Watch "abstract classes??" New topic

abstract classes??

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
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 whats the use of having of constructors when we are

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

somewhere...someone please clarify ......

Thanks & Regards<br /> <br />-Srikanth
Ramesh Mangam

Joined: May 03, 2005
Posts: 13

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

Joined: Oct 12, 2005
Posts: 54
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

Joined: Aug 18, 2005
Posts: 220
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.

"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Ray Horn
Ranch Hand

Joined: Oct 20, 2005
Posts: 39
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.
I agree. Here's the link:
subject: abstract classes??
It's not a secret anymore!