0

Say I have a parent class Emitter, with attributes

private double launchAngle;
private double launchAngleVariation;

and a constructor

public Emitter(double launchAngle, double launchAngleVariation)
{ this.launchAngle = launchAngle;
  this.launchAngleVariation = launchAngleVariation;
}

and a child class MobileEmitter, with attribute

private int a;

and a constructor

public MobileEmitter(double launchAngle, int a)
{ super();   //how should this be handled?
  this.a = a;}

My question is what is the best way to handle the super call?

Should I create an empty constructor in the parent class and make launchAngle protected so that I can assign the value directly in the child constructor?

Or is it better practice to create a separate constructor in the parent class with just launchAngle as a parameter then call super(launchAngle) ?

sBourne
  • 483
  • 1
  • 5
  • 17
  • This is more of a style question than a code issue, so is possibly better suited to another stackexchange site. Personally I'd say create a constructor `Emitter(launchAngle)` - as emitter owns the field launchAngle it feels better that emitter does the setting of that field – beresfordt Mar 10 '16 at 19:42
  • You may want to submit your code to [code review](http://codereview.stackexchange.com). I wouldn't add a constructor in `Emitter` only for this reason if it does not make sense. I wouldn't set the attribute to `protected` either. I would rather use a protected setter or `super(launchAngle,0)`. – rdupz Mar 10 '16 at 19:50
  • What is the `MobileEmitter`'s launch angle variation? What does `a` stand for? – 200_success Mar 10 '16 at 20:00
  • Its just an arbitrary parameter local to `MobileEmitter` for the purpose of demonstration – sBourne Mar 10 '16 at 20:07
  • @standelaune Calling an overrideable methods (ie the protected setter) in a constructor opens you up to many quite unpleasant bugs: http://stackoverflow.com/a/3404369/658663 – beresfordt Mar 10 '16 at 20:14
  • @beresfordt I think problems can appear when the overridable method is in the parent constructor, not in the child's one. – rdupz Mar 11 '16 at 08:00
  • @standelaune yes, but can you be sure of the future uses of this class, or the way in which other people use your artifact? no point in leaving yourself open to those kind of issues in my opinion – beresfordt Mar 11 '16 at 08:42
  • @beresfordt setters are unlikely to be overidden but generally speaking you're totally right ;). – rdupz Mar 11 '16 at 11:27
  • @standelaune you wouldn't believe some of the horrors I've seen at my current gig.. :o – beresfordt Mar 11 '16 at 11:29

1 Answers1

0
private int a;

public MobileEmitter(double launchAngle, double launchAngleVariation, int a) {
    super(launchAngle, launchAngleVariation);
    this.a = a;
}

This is what I think you would need. The super call must have the some values assigned to it. In your case you are calling the default constructor found in the Emitter class (parent class).

Monroy
  • 420
  • 5
  • 19