How to force setting a varible in sublcass when it is defined in superclass ?

J

jlukar

eg.


abstract MySuperCalss {
Logger log someLogger;
public void mymethod() { }
}

MySubClass extends MySuperClass {
publlic void someMethod{
someLogger.info("Hello");
}

if I don't take care to instanticate someLogger, it will throw
NullPointerException in MySubClass.someMethod();

I can't define someLogger as abstract neither.

note that I can have many subclasses, so making a just a mental note to
set it in every subclass is not good enough. I'd like the compiler
to catch it.



thanks
j.
 
S

shakah

eg.

abstract MySuperCalss {
Logger log someLogger;
public void mymethod() { }
}

MySubClass extends MySuperClass {
publlic void someMethod{
someLogger.info("Hello");
}

if I don't take care to instanticate someLogger, it will throw
NullPointerException in MySubClass.someMethod();

I can't define someLogger as abstract neither.

note that I can have many subclasses, so making a just a mental note to
set it in every subclass is not good enough. I'd like the compiler
to catch it.

thanks
j.

the following:

public class MySuper {
Logger l_ ;
public MySuper(Logger l) {
l_ = l ;
}
}

would force your subclasses to at least consciously set the logger,
e.g.:

// ...produces a compilation error
public class MySub : extends MySuper {
public MySub() {
}
}

// ...requires syntax like:
public class MySub extends MySuper {
public MySub() {
super(new Logger()) ;
}
}

// ...or
public class MySub extends MySuper {
public MySub(Logger l) {
super(l) ;
}
}

// ...etc.
 
R

Rhino

eg.


abstract MySuperCalss {
Logger log someLogger;
public void mymethod() { }
}

MySubClass extends MySuperClass {
publlic void someMethod{
someLogger.info("Hello");
}

if I don't take care to instanticate someLogger, it will throw
NullPointerException in MySubClass.someMethod();

I can't define someLogger as abstract neither.

note that I can have many subclasses, so making a just a mental note to
set it in every subclass is not good enough. I'd like the compiler
to catch it.



thanks
j.

If it were my program, I would pass the logger to the constructor of the
subclass so that it is available to the subclass's method when it is needed.
Any code that instantiate the subclass would then have to supply a value for
the Logger or it would cause a compiler error.

Now, the value passed in the statement that instantiates the subclass could
pass a null value for the Logger and that would satisfy the compiler. I
would deal with that problem by checking the Logger value in the constructor
of the subclass; if it is null, throw an IllegalArgumentException. That, of
course, is a runtime error, not a compile error, but I don't know of any
better solution.

Rhino
 
O

Oliver Wong

note that I can have many subclasses, so making a just a mental note to
set it in every subclass is not good enough. I'd like the compiler
to catch it.

shakah gave a good solution. Here's an alternative:

<code>
abstract class MySuperClass {
protected abstract Logger getLogger();
}

class MySubClass extends MySuperClass {
private Logger myLogger = new Logger();

public void someMethod{
this.getLogger().info("Hello");
}

protected Logger getLogger() {
return this.myLogger;
}
}
</code>

The abstract getter is usually a good reminder for implementors of
subclasses that they MUST create a logger.

- Oliver
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top