0

I am currently learning about inheritance in Java and I am having a great amount of trouble understanding it, however one of the main things I cant understand is why is it necessary to invocate a superclass constructor in the following example and how does it help? This example is right from Oracle's tutorial for the super(...) method.

public MountainBike(int startHeight, 
                int startCadence,
                int startSpeed,
                int startGear) {
  super(startCadence, startSpeed, startGear);
  seatHeight = startHeight;
}

What I was thinking originally was that super(startCadence, startSpeed, startGear); would shorten the code for the parameters in my current constructor and it would simply take the values that were passed in in the parent class and add it to the child class (like this):

public MountainBike(int startHeight) {
 ... 
}

however as I already know that's wrong, and once again brings me to the question of what super(startCadence, startSpeed, startGear); really does. I would greatly appreciate some form of explanation so a beginner, like me, could actually understand rather than giving me the definition.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

4

Because MountainBike is a child of (some class like) Bike, it should be usable in any context that requires a Bike object. If you don't call the parent class's constructor in the child's, then there is no guarantee that the child object is initialized properly to act as an instance of the parent class.

I vaguely recall that in Java, if you don't give an explicit call to the parent's constructor in the child's constructor, the default constructor for the parent (i.e., with no arguments) is called implicitly. In your case, that isn't enough, because there either is no default constructor, or you really need to pass the extra arguments given to the child's constructor on to the parent's constructor.

chepner
  • 497,756
  • 71
  • 530
  • 681
1

Let's assume that your class, MountainBike inherits from Bike. The word super is a reference to a class's parent class.

If Bike has a constructor which looks like this:

public Bike(int startCadence, int startSpeed, int startGear) {
    cadence = startCadence;
    speed = startSpeed;
    gear = startGear;
}

Then the MountainBike constructor which looks like this:

public MountainBike(int startCadence, int startSpeed, int startGear, int startHeight) {
    super(startCadence, startSpeed, startGear);
    seatHeight = startHeight;
}

is almost equivalent to this:

public MountainBike(int startCadence, int startSpeed, int startGear, int startHeight) {
    super.cadence = startCadence;
    super.speed = startSpeed;
    super.gear = startGear;
    seatHeight = startHeight;
}

Except the former, where we call the super constructor is better. For one, it avoids duplicating code. For two, it saves two lines. For three, if I need to include some verification logic on speed for example, I can just include it in the Bike constructor and don't need to duplicate that code in the MountainBike constructor as well.

chepner's answer explains some other reasons why the second version can be problematic. But this answer hopefully helps explain what the call to the super constructor is doing.

nhgrif
  • 61,578
  • 25
  • 134
  • 173