Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Abstract Classes w/o abstract methods

Reply
Thread Tools

Abstract Classes w/o abstract methods

 
 
DaKoadMunky
Guest
Posts: n/a
 
      04-20-2004
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


 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      04-20-2004
DaKoadMunky wrote:
> 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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      04-20-2004
On 20 Apr 2004 02:04:32 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed) (DaKoadMunky) wrote
or quoted :

>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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      04-20-2004
> 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


 
Reply With Quote
 
Lee Weiner
Guest
Posts: n/a
 
      04-20-2004
In article <(E-Mail Removed)>, (E-Mail Removed) (DaKoadMunky) wrote:
>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
methods, methods override classes and scope Ramza Brown Ruby 1 04-18-2006 07:38 AM
Vector of abstract classes filled with derived classes ? Manuel C++ 8 01-05-2006 09:07 PM
Re: Abstract Methods & Abstract Class Gerald Klix Python 1 10-20-2005 02:44 PM
Abstract Methods & Abstract Class Iyer, Prasad C Python 0 10-20-2005 06:35 AM



Advertisments