Abstract Classes w/o abstract methods

Discussion in 'Java' started by DaKoadMunky, Apr 20, 2004.

  1. DaKoadMunky

    DaKoadMunky Guest

    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
    DaKoadMunky, Apr 20, 2004
    #1
    1. Advertising

  2. DaKoadMunky

    Chris Smith Guest

    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
    Chris Smith, Apr 20, 2004
    #2
    1. Advertising

  3. DaKoadMunky

    Roedy Green Guest

    On 20 Apr 2004 02:04:32 GMT, (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.
    Roedy Green, Apr 20, 2004
    #3
  4. DaKoadMunky

    Tony Morris Guest

    > 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
    Tony Morris, Apr 20, 2004
    #4
  5. DaKoadMunky

    Lee Weiner Guest

    In article <>, (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
    Lee Weiner, Apr 20, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Abstract Methods & Abstract Class

    Iyer, Prasad C, Oct 20, 2005, in forum: Python
    Replies:
    0
    Views:
    532
    Iyer, Prasad C
    Oct 20, 2005
  2. Gerald Klix

    Re: Abstract Methods & Abstract Class

    Gerald Klix, Oct 20, 2005, in forum: Python
    Replies:
    1
    Views:
    343
    =?iso-8859-1?B?c+liYXN0aWVu?=
    Oct 20, 2005
  3. Manuel
    Replies:
    8
    Views:
    639
    Manuel
    Jan 5, 2006
  4. Ramza Brown
    Replies:
    1
    Views:
    115
  5. Kenneth McDonald
    Replies:
    5
    Views:
    313
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page