Newbie Post: Question about Inheritance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • billelev
    New Member
    • Nov 2006
    • 119

    #1

    Newbie Post: Question about Inheritance

    Hi there,

    I've tried to look into this issue but I'm having difficulty finding a solution.

    I have two classes: PaymentDate and its subclass PaymentDateRef. PaymentDate has a method setMaturityPeri od that is called in the PaymentDate constructor, which ultimately sets the variable maturityPeriod.

    In PaymentDateRef I have written a method setMaturityPeri od which I believed should override the superclass method of the same name.

    Now, when I call the constructor of PaymentDateRef I must also reference the constructor of PaymentDate through the super keyword. However, what I am seeing is that this then calls setMaturityPeri od within PaymentDate and then from within the constructor of PaymentDateRef. ..i.e. the setMaturityPeri od method is called twice.

    public PaymentDate() {
    setMaturityPeri od();
    }


    public PaymentDateRef( ) {
    super();
    setMaturityPeri od();
    }

    Does anyone know how I can call setMaturityPeri od only once from within the constructor? I thought the override property would cause the new setMaturityPeri od to be called rather than the old, allowing the PaymentDateRef constructor to be used:

    public PaymentDateRef( ) {
    super();
    }

    but in this case only the original setMaturityPeri od method is called.
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by billelev
    Hi there,

    I've tried to look into this issue but I'm having difficulty finding a solution.

    I have two classes: PaymentDate and its subclass PaymentDateRef. PaymentDate has a method setMaturityPeri od that is called in the PaymentDate constructor, which ultimately sets the variable maturityPeriod.

    In PaymentDateRef I have written a method setMaturityPeri od which I believed should override the superclass method of the same name.

    Now, when I call the constructor of PaymentDateRef I must also reference the constructor of PaymentDate through the super keyword. However, what I am seeing is that this then calls setMaturityPeri od within PaymentDate and then from within the constructor of PaymentDateRef. ..i.e. the setMaturityPeri od method is called twice.

    public PaymentDate() {
    setMaturityPeri od();
    }


    public PaymentDateRef( ) {
    super();
    setMaturityPeri od();
    }

    Does anyone know how I can call setMaturityPeri od only once from within the constructor? I thought the override property would cause the new setMaturityPeri od to be called rather than the old, allowing the PaymentDateRef constructor to be used:

    public PaymentDateRef( ) {
    super();
    }

    but in this case only the original setMaturityPeri od method is called.

    Can you post the whole you written with code tags.
    And what you need and what is wrong with you?

    Kind regards,
    Dmjpro.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by billelev
      Hi there,

      I've tried to look into this issue but I'm having difficulty finding a solution.

      I have two classes: PaymentDate and its subclass PaymentDateRef. PaymentDate has a method setMaturityPeri od that is called in the PaymentDate constructor, which ultimately sets the variable maturityPeriod.

      In PaymentDateRef I have written a method setMaturityPeri od which I believed should override the superclass method of the same name.

      Now, when I call the constructor of PaymentDateRef I must also reference the constructor of PaymentDate through the super keyword. However, what I am seeing is that this then calls setMaturityPeri od within PaymentDate and then from within the constructor of PaymentDateRef. ..i.e. the setMaturityPeri od method is called twice.

      public PaymentDate() {
      setMaturityPeri od();
      }


      public PaymentDateRef( ) {
      super();
      setMaturityPeri od();
      }

      Does anyone know how I can call setMaturityPeri od only once from within the constructor? I thought the override property would cause the new setMaturityPeri od to be called rather than the old, allowing the PaymentDateRef constructor to be used:

      public PaymentDateRef( ) {
      super();
      }

      but in this case only the original setMaturityPeri od method is called.
      If a ctor calls a method from its own class that is not private (either directly or
      indirectly) it is a serious indication of bad design if that class is intended to be
      extended by subclasses. In the Java Articles section there's an article about the
      Liskov Substitution Principle. It is right on target w.r.t. your problem.

      kind regards,

      Jos

      Comment

      Working...