简体   繁体   中英

Access variable in parent class using super(), or an object?

If you need any info or have a link to a solved answer feel free to post it and I will adjust/delete my question.

So for an assignment in comp sci I need to access variables in a parent class. I have something like

//import anything?


public class HW6Fraction extends Fraction{
  public HW6Fraction(int num, int denom){
    super();
  }

  public Fraction add(Fraction f) { //Add method
    int num = super().getNumerator + f.getNumerator();
    int denom = super().getDenominator + f.getDenominator();
    Fraction result = new Fraction(num, denom);
    return result;
  }

  public Fraction subtract(Fraction f) { //Subtract method
    int num = super().getNumerator - f.getNumerator();
    int denom = super().getDenominator - f.getDenominator();
    Fraction result = new Fraction(num, denom);
    return result;
  }
}

as the subclass and something like this as the parent class:

//Assignment 2 - problem 3, p. 125
/*
  Add, subtract, multiply, and divide methods to the Fraction class
  (section 3.8).  Also, add two more constructors.  One of the constructors
  will have no parameters; it initializes the fraction to 0/1.  The other
  constructor will have one parameter, representing the numerator of the
  fraction; the denominator of the fraction will be 1.  Write a TestFraction
  class (similar to the TestAccount class of Section 3.6) that creates two
  Fraction objects and tests the behavior of all constructors and instance
  methods in the Fraction class.

    Logic:
    Addition - ((numerator of fraction 1 * denominator of fraction 2) +
                (numerator of fraction 2 * denominator of fraction 1)) /
                (denominator of fraction 1 * denominator of fraction 2)

    Subtraction - ((numerator of fraction 1 * denominator of fraction 2) -
                  (numerator of fraction 2 * denominator of fraction 1)) /
                  (denominator of fraction 1 * denominator of fraction 2)

    Multiplication - (numerator of fraction 1 * numerator of fraction 2) /
                     (denominator of fraction 1 * denominator of fraction 2)

    Division - (numerator of fraction 1 * denominator of fraction 2) /
               (denominator of fraction 2 * numerator of fraction 1)
*/


public class Fraction {
  private int numerator; //Numerator of fraction
  private int denominator; //Denominator of fraction

  public Fraction(int num, int denom) { //Constructor
    numerator = num;
    denominator = denom;
  }

  public Fraction() { //Constructor w/ no parameters
    new Fraction(0, 1);
  }

  public Fraction(int num) { //Constructor w/ numerator parameter
    numerator = num;
    int denom = 1;
  }

  public int getNumerator() { //getNumerator method
    return numerator;
  }

  public int getDenominator() { //getDenominator method
    return denominator;
  }

  public void setNumerator(int num) { //setNumerator method
    numerator = num;
  }

  public void setDenominator(int denom) { //setDenominator method
    denominator = denom;
  }

  public Fraction add(Fraction f) { //Add method
    int num = numerator * f.getDenominator() + f.getNumerator() *
              denominator;
    int denom = denominator * f.getDenominator();
    Fraction result = new Fraction(num, denom);
    return result;
  }

  public Fraction subtract(Fraction f) { //Subtract method
    int num = numerator * f.getDenominator() - f.getNumerator() *
              denominator;
    int denom = denominator * f.getDenominator();
    Fraction result = new Fraction(num, denom);
    return result;
  }

  public Fraction multiply(Fraction f) { //Multiply method
    int num = numerator * f.getNumerator();
    int denom = denominator * f.getDenominator();
    Fraction result = new Fraction(num, denom);
    return result;
  }

  public Fraction divide(Fraction f) { //Divide method
    int num = numerator * f.getDenominator();
    int denom = denominator * f.getNumerator();
    Fraction result = new Fraction(num, denom);
    return result;
  }
}

These lines are where I am having a problem:

int num = super().getNumerator + f.getNumerator();

I wonder if there is proper terminology for this?

Your HW6Fraction class inherits the method from its parent class

So you can just write:

int num = getNumerator() + f.getNumerator();

If your HW6Fraction also declares a getNumerator() method, and you want to rather call the getNumerator() defined in the parent class you do:

int num = super.getNumerator() + f.getNumerator();

getNumerator is a public function, so anything that has a reference to your class should be able to call it, including all child classes.

When you inherit from a class you inherit all of its public and protected members, so they should all be part of the child class that you're working with. Technically you don't need to put anything special in front just calling getNumerator() should work fine.

Alternatively, this.getNumerator() will explicitly state that you want the getNumerator function associated with the object that is currently executing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM