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

Discussion in 'Java' started by jlukar@gmail.com, Nov 7, 2005.

  1. Guest

    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.
    , Nov 7, 2005
    #1
    1. Advertising

  2. shakah Guest

    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.
    shakah, Nov 7, 2005
    #2
    1. Advertising

  3. Rhino Guest

    <> wrote in message
    news:...
    >
    > 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
    Rhino, Nov 7, 2005
    #3
  4. Oliver Wong Guest

    <> wrote in message
    news:...
    >
    > 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
    Oliver Wong, Nov 7, 2005
    #4
    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. Inyeol Lee

    need help on sublcass and scope

    Inyeol Lee, Dec 9, 2003, in forum: Python
    Replies:
    1
    Views:
    289
    Peter Otten
    Dec 9, 2003
  2. Skip Montanaro

    pickling sublcass of gobject.GObject?

    Skip Montanaro, Jul 27, 2004, in forum: Python
    Replies:
    0
    Views:
    369
    Skip Montanaro
    Jul 27, 2004
  3. Evan Klitzke
    Replies:
    0
    Views:
    360
    Evan Klitzke
    Aug 2, 2007
  4. bart van deenen
    Replies:
    6
    Views:
    749
    bart van deenen
    Mar 3, 2009
  5. Sam Takoy

    "this" pointer in a sublcass

    Sam Takoy, Mar 16, 2010, in forum: Java
    Replies:
    8
    Views:
    1,223
    Roedy Green
    Mar 17, 2010
Loading...

Share This Page