Inconvenience due to abstract class does not have a constructor

W

www

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

Lew

www said:
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();
}

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

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.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top