Abstract Classes w/o abstract methods

D

DaKoadMunky

It is my understanding that an abstract class need not declare abstract
methods.

My question is this....

If an abstract class does not force a contractual obligation between itself and
its subclasses using the abstract method mechanism, then what value is there in
making the class abstract?

I would appreciate any insight as to how that could be useful.

Regards,
Brian
 
C

Chris Smith

DaKoadMunky said:
It is my understanding that an abstract class need not declare abstract
methods.

My question is this....

If an abstract class does not force a contractual obligation between itself and
its subclasses using the abstract method mechanism, then what value is there in
making the class abstract?

I would appreciate any insight as to how that could be useful.

It's just a self-documenting code thing. If you've written a class that
provides a framework whose methods can be overridden to do useful work
-- but is completely useless on its own -- then you can make it abstract
to make it clear to others that you intend for them to subclass it and
override methods. java.awt.Component is a great example.

For an even more interesting example, see java.awt.FontMetrics. Though
it doesn't declare any abstract methods, the default implementations
result in infinite recursion. The 'abstract' modifier for the class
doesn't prevent anyone from subclassing and then failing to override
enough methods to prevent the infinite recursion... but at least it tips
you off that you aren't supposed to create an instance of FontMetrics
directly. From that point, the documentation fills in the details.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

If an abstract class does not force a contractual obligation between itself and
its subclasses using the abstract method mechanism, then what value is there in
making the class abstract?

It is basically a flag warning you that buried in there somewhere is a
abstract method that would make the whole class abstract.
 
T

Tony Morris

If an abstract class does not force a contractual obligation between
itself and
its subclasses using the abstract method mechanism, then what value is there in
making the class abstract?


To prevent instantiation such that clients are forced to subclass (as you
intend).

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
L

Lee Weiner

It is my understanding that an abstract class need not declare abstract
methods.

My question is this....

If an abstract class does not force a contractual obligation between itself and
its subclasses using the abstract method mechanism, then what value is there in
making the class abstract?

I would appreciate any insight as to how that could be useful.

Declaring a class abstract simply means an object of that class cannot be
created. As to why you do it, an abstract class can provide a base from which
a family of subclasses can be built. Suppose I need a car class and a truck
class. Eighty percent of the fields in each are the same, and obviously, 20%
of each are different. I can put the 80% in the abstract class, and each
subclass, car and truck, can add their own 20%. Neither class contains any
fields it doesn't need. I don't want to allow the abstract base class to be
instantiated because, by itself, it doesn't contain enough data to represent
any kind of vehicle.

Lee Weiner
lee AT leeweiner DOT org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top