Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to force setting a varible in sublcass when it is defined in superclass ?

Reply
Thread Tools

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

 
 
jlukar@gmail.com
Guest
Posts: n/a
 
      11-07-2005

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.

 
Reply With Quote
 
 
 
 
shakah
Guest
Posts: n/a
 
      11-07-2005
(E-Mail Removed) wrote:
> 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.

 
Reply With Quote
 
 
 
 
Rhino
Guest
Posts: n/a
 
      11-07-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> 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


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      11-07-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> 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


 
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
How to check if another object is my superclass from a function in aneven higher superclass? bart van deenen C++ 6 03-03-2009 02:44 PM
Nike air force one, air force 1, air force one low cut, air force one abdul_razak@indiatimes.com Digital Photography 2 12-31-2008 04:29 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:46 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:34 PM
calling superclass __init__ when superclass is object Evan Klitzke Python 0 08-02-2007 05:15 AM



Advertisments