0

So I have this code, which is the parent class:

public class Score {

  private int baseScore;

  public Score() {}

  public Score(int baseScore) {
    this.baseScore = baseScore;
  }

  public int increaseScore(int value) {
    this.baseScore += value;
    return this.baseScore;
  }

  public int computeScore(int newLevelPoints) {
    return this.increaseScore(newLevelPoints);
  }

  public int computeScore(int newLevelPoints, int bonus) {
    return this.increaseScore(newLevelPoints + 2 * bonus);
  }
}

And the child class is this:

public class GameScore extends Score {

  public GameScore() {}

  public GameScore(int baseScore) {
    super(baseScore);
  }

  @Override
  public int computeScore(int newLevelPoints, int bonus) {
    return super.increaseScore(newLevelPoints + 10 * bonus);
  }

  public int computeScore(int bonus) {
    return super.increaseScore(10 * bonus);
  }
}

I don't understand which method is called when I do the following in main:

public class Test {
  public static void main(String[] args) {
    int newLevelPoints = 10;
    int bonus = 2;

    Score score = new GameScore();

   //1st. score.computeScore(newLevelPoints);
   //2nd. score.computeScore(newLevelPoints, bonus);
   //3rd. System.out.println(score.computeScore(bonus));
  }
}
  1. So basically, in the 1st, should the method from SCORE class be the one that executes? And shouldn't the answer be: 10?

  2. The second method is accessing the method from parent class no? I mean, if the reference is from the parent class, it will access the method from a child because it is overwritten or will access the methods from the parent (even though the object is from the child class)?

  3. Because I'm not sure what to call in the first 2 methods. I have a hard time making sense of the third.. basically, the answer for 3 should be 150.

kelalaka
  • 4,611
  • 5
  • 25
  • 40
  • To [debug the thing](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?noredirect=1&lq=1) could give you a great deal of insight, and going through some tutorials on OO and inheritance. – Curiosa Globunznik Oct 13 '19 at 06:48
  • Welcome to stackoverflow.com btw. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask). Finally please read this [question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/) – Curiosa Globunznik Oct 13 '19 at 06:49

0 Answers0