2

I want to replace some methods in DefaultArrowButton and JComboBox to make them visual changes (change everything). Some methods are private. I can't override them. Next problem is lack of possibility to call private members of superclass.

I am sure I have wrong idea to achieve my goal. I putted some code below which is wrong because I can't acces private members from child Class. Also I didn't insert all long code just two methods which show everything I want to show (+ constructors):

public class DefaultArrowButton extends BasicArrowButton {

    public DefaultArrowButton(int direction) {
        super(direction);
        setBackground(HFColors.BLUE);
        setBorder(BorderFactory.createEmptyBorder(0,0,0,0));

    }

    public DefaultArrowButton(int direction, Color background, Color shadow, 
            Color darkShadow, Color highlight) {
        super(direction, background, shadow, darkShadow, highlight);
    }

    public void paintTriangle(Graphics g, int x, int y, int size, int direction,
                          boolean isEnabled)
    {
      Color savedColor = g.getColor();
      switch (direction)
        {
        case NORTH:
          paintTriangleNorth(g, x, y, size, isEnabled);
          break;
        case SOUTH:
          paintTriangleSouth(g, x, y, size, isEnabled);
          break;
        case LEFT:
        case WEST:
          paintTriangleWest(g, x, y, size, isEnabled);
          break;
        case RIGHT:
        case EAST:
          paintTriangleEast(g, x, y, size, isEnabled);
          break;
        }
      g.setColor(savedColor);
    }

    private void paintTriangleSouth(Graphics g, int x, int y, int size, 
            boolean isEnabled) {
        int tipX = x + (size - 2) / 2;
        int tipY = y + (size - 1);
        int baseX1 = tipX - (size - 1);
        int baseX2 = tipX + (size - 1);
        int baseY = y;
        Polygon triangle = new Polygon();
        triangle.addPoint(tipX, tipY);
        triangle.addPoint(baseX1, baseY);
        triangle.addPoint(baseX2, baseY);
        if (isEnabled)
        {
            g.setColor(Color.CYAN);
            g.fillPolygon(triangle);
            g.drawPolygon(triangle);
        } else
        {
            g.setColor(Color.CYAN);
            g.fillPolygon(triangle);
            g.drawPolygon(triangle);
        }
    }
}

What should I do instead?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
arrowman
  • 424
  • 6
  • 19
  • If my question is not suitable for this forum or generally is wrong, please could you write me where should I write and how to ask properly? – arrowman Jan 01 '20 at 10:15
  • Please [edit] your question to explain in detail what you are trying to in the first place. This sounds like a "XY problem" where your original problem might be solved easier. Maybe add screenshots of the button you want to build. Also, if possible provide a [mcve] of the problem you have. – Progman Jan 01 '20 at 17:29

1 Answers1

1

There are two question here.

  1. Invoking parent's private methods

Yes, you can invoke parent's private methods. Please check How to invoke parent private method from child? for how. Now the question is, is it okay to do that. It's ok to do that only if the method doesn't have any side-effects. Please check Side effect--what's this? to know about method side-effects.

  1. Overriding parent's private methods

No, it's not possible to override parent's private method. Please check this Override "private" method in java.

So in your case, if you just want to invoke parent's private method, make sure that method doesn't have any side-effects. But if you want to override paren'ts private method, create a new class from scratch.

BHAWANI SINGH
  • 729
  • 4
  • 8
  • I am not professional programmer. So, what is your suggestion to do better (more professional) : 1. write subclass like you mentioned (less clear code, less work). 2. write whole own class for replacing BasicArrowButton (more work - maybe more clear). – arrowman Jan 01 '20 at 13:28
  • 1
    @arrowman I will suggest to honor the design principle _**Composition over inheritance**_ (so _2nd_ approach), by keeping an instance of parent class in your new class, instead of extending it. You may call the public methods of composite class when required, otherwise you use your implemented methods. – BHAWANI SINGH Jan 01 '20 at 16:42
  • Could you post it? I will make it as reply. Thanks! – arrowman Jan 01 '20 at 18:13
  • @arrowman have went through the question again, and edited my answer, please chem it out. In short if your requirement is to override parent's private method, that can't be done, and it shouldn't be done. I think you need to review your requirements and your design. Thanks for the question. – BHAWANI SINGH Jan 01 '20 at 20:34