0

I can't believe I didn't find anything here for this Question, so:

Why do I need to explicitly define all Constructors of a superclass when deriving a class. Why does Java not automatically call the super constructor, when the derived class doesn't offer one with that signature.

That would be some nice piece of "Syntactic Sugar".

E.g: In Android each View Subclass offers 3 Constructors that are called either from XML or from Code. To be able to create a custom View in Code and in XML I need to define all three constructors, even if they don't do anything. Here is a small example I had to write yesterday

public class OverfocusableGridView extends GridView {

    public OverfocusableGridView(Context context) {
        super(context);
    }

    public OverfocusableGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public OverfocusableGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean keyHandled = false;
        if (this.getSelectedView() != null) {
            int focusedViewPosition = this.getSelectedItemPosition();
            switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                if (focusedViewPosition > 0) {
                    int prevItemPosition = focusedViewPosition - 1;
                    this.setSelection(prevItemPosition);
                    keyHandled = true;
                }
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                if (focusedViewPosition < this.getChildCount() - 1) {
                    int nextItemPosition = focusedViewPosition + 1;
                    this.setSelection(nextItemPosition);
                    keyHandled = true;
                }
                break;
            default:
                keyHandled = super.onKeyDown(keyCode, event);
            }
        } else {
            keyHandled = super.onKeyDown(keyCode, event);
        }
        return keyHandled;
    }

}

Would be much nicer if I would not need to define theese Constructors. Are there any downsides with this approach or are there some Java Design decisions that explains that?

So, Long story short: Is this not possible for a reason?

Community
  • 1
  • 1
chuck258
  • 912
  • 7
  • 16
  • Yeah thanks, I didn't find it because I searched for derived.... forgot about the word inheritance :) – chuck258 Nov 06 '13 at 08:38

2 Answers2

2

Suppose constructors were inherited... then because every class eventually derives from Object, every class would end up with a parameterless constructor. That's a bad idea. What exactly would you expect:

FileInputStream stream = new FileInputStream();

to do?

Now potentially there should be a way of easily creating the "pass-through" constructors which are fairly common, but I don't think it should be the default. The parameters needed to construct a subclass are often different from those required by the superclass.


The answer was taken from: https://stackoverflow.com/a/1644410/1724097

With out the word "inherit" its hard to find the other question, so i understand why you didnt find your answer (:

Community
  • 1
  • 1
Rawa
  • 13,357
  • 6
  • 39
  • 55
  • For clarification i copied the answer so if the old link gets removed it doesn't affect the answer of this one. – Rawa Nov 06 '13 at 08:41
0

A Java subclass need not to have equal constructors to the super class! Very often a subclass have fewer and/or more simple constructors.

Do you really need all the constructors?

Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94
  • Jeah I need them. I create the View in Code with the Context only Constructor and the Context&AttributeSet gets called while parsing my XML Layout. Ok, the third one could be left away, but my Question was not for this particular piece of code – chuck258 Nov 06 '13 at 08:41