Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Inconvenience due to abstract class does not have a constructor

Thread view: 
www - 22 Oct 2007 16:59 GMT
Hi,

I have an Abstract class and other classes which are subclasses of it.

public abstract MyClass {
    protected List _list;

    public abstract void doIt();
}

public class ClassA extends MyClass {

    public ClassA() {
        _list = new ArrayList(); //this is mandatory since _list is not
implemented YET!
        ...//other things
    }
   
    public void doIt() {
        double d = getList().get(1);  //using super_list
    }
    ..
}

public class ClassB extends MyClass {

    public ClassB() {
        _list = new ArrayList(); //this is mandatory since _list is not
implemented YET!
        ...//other things
    }

    ..
}

It is kind of hard or tricky for anybody to remember, if you extend your
class from MyClass, be sure to add "_list = new ArrayList()". Actually,
I didn't know such a "requirement" and got null point exception when I
ran my program.

Should I do like this:

public abstract MyClass {
    protected List _list = new ArralyList(); //now, the subclass no need to
implement it in their constructors

    public abstract void doIt();
}

Thank you for your help.

Thank you very much.
Lew - 22 Oct 2007 17:11 GMT
> I have an Abstract class and other classes which are subclasses of it.
>
> public abstract MyClass {
>     protected List _list;

You should not expose implementation details to subclasses.  Make this
variable private.

Don't use underscores in names for non-constant (i.e., non-final or mutable)
variablea.

Instance variables should be initialized in the declaring class.  You cannot
count on a subclass implementor to do this correctly.

>     public abstract void doIt();
> }
[quoted text clipped - 9 lines]
>     public void doIt() {
>         double d = getList().get(1);  //using super_list

You do not show a getList() method.  Where does this come from?

>     }
>     ..
> }

> It is kind of hard or tricky for anybody to remember, if you extend your
> class from MyClass, be sure to add "_list = new ArrayList()". Actually,
> I didn't know such a "requirement" and got null point exception when I
> ran my program.

Not only is it not a requirement, it's terrible coding to set things up where
an instance variable is instantiated only by a subclass.

The point of an abstract class is to fix certain aspects of the
implementation.  There is no such thing as an "abstract instance variable".

> Should I do like this:
>
> public abstract MyClass {
>     protected List _list = new ArralyList(); //now, the subclass no need
> to implement it in their constructors

They should *never* need to implement a superclass variable.

>     public abstract void doIt();
> }

Yes.

Signature

Lew

Lew - 22 Oct 2007 17:12 GMT
> [complete lack of SSCCE]

You've been around the newsgroup long enough to know about
<http://www.physci.org/codes/sscce.html>

Why did you refuse to do that?  Your example leaves out significant details.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.