简体   繁体   中英

Using a method of parent class with variable of child class without overriding

I am currently designing two classes in Java, and am having some trouble with understanding my current problem of inheritance, so I've created a simple example code in hopes that someone can help me understand what is going on.

So in this example, a parent class (for general purpose, we could call it class Car) and a child class (class RedCar). The parent class, which is abstract, contains an attribute distance, and contains a method of computeVelocity, which takes input argument time (double), and returns a double value of distance divided by time (thus giving velocity).

The child class has a constructor, RedCar(double inputDistance). It should also have access to the computeVelocity method, without overriding it.

Here's where the problem is. Logically, I understand that computeVelocity should not take the inputDistance variable from RedCar because its calling a method of another class. What I want to know, however, is how to bring that method into the RedCar class so that it could be used within said class with the arguments handed to the constructor.

Here is an example of the code

//Car Class
public abstract class Car{
  //abstract variable
  private double distance;
  //compute velocity method
  public double computeVelocity(double time){
     return distance/time;
  }
 }




 //RedCar Class
 public class RedCar extends Car{
   public double distance;

   public RedCar (double inputDistance){
      this.distance = inputDistance;
   }
  //This is currently my method but I believe this already constitutes as overridding
  public double computeVelocity(double hours){
      Car.super.computeVelocity(hours);
  }
 }

As stated in some of the comments to you answer, you don't really need and shouldn't have the distance twice (once in the parent class and once in the child class). Use the distance parameter from the parent class and there is no reason to override your method. You can call your parent class method using the super keyword.

Hope this helps.

Since the private instance variable is inherited in the subclass but not accessible in subclass(You can use the reflection to access it). You have two way to provide value in Car instance variable private double distance by providing the public setter or getter method or provide the protected or public constructor and call that constructor from the subclass.

See the link Do subclasses inherit private fields? inherit-private-fields

Constructor in the super class which set the value in the private distance variable because private member only accessible by there own member means by their own method and constructor.

    protected Car(double distance){
     this.distance = distance;
     }

And also you have no need to define the distance variable again in the subclass because you are not using it in your method computeVelocity (of subclass). Since subclass method call the super class method and the super class method use own private double distance. So your distance variable define in subclass not used by the super class method.

After providing the protected constructor in super class you can call it from subclass constructor using the super keyword.The super keyword call the constructor of the super class. Since we provide the constructor in super class so java not add any default constructor in super class. And calling that constructor from sub class using super assign the value in private double distance variable

     public RedCar(double inputDistance) {
     super(inputDistance);

      }

And correct way to call the super class method is, no need Car.super

    public double computeVelocity(double hours) {
      return super.computeVelocity(hours);
     }

Your class RedCar inherits class Car . So Car is not just another class. If you create a RedCar instance, all parts of Car are also available. A RedCar is a Car .

In your solution you have created a field distance both in the super class and in the subclass. Actually you have two fields named distance in each RedCar instance.

Here comes the visibility into account. Though the private variable distance is part of RedCar it is only visible in class Car . If only methods defined in Car needs to access distance there is no need to change the visibility. But you need a way to set the distance . If the value doesn't change over time, you should include a constructor in class Car .

The visibility of method computeVelocity() defined in Car is public, so there is no need to repeat it in the subclass. Because a RedCar is a Car you can call computeVelocity() on each RedCar instance.

public abstract class Car{
  private double distance;

  // Constructor with visibility protected.
  // So it is visible in each subclass and must be 
  // called by each subclass constructor.
  // Btw.: It is common practice to use the same name
  // for input parameters as for fields. The field
  // variable can be accessed with the "this" keyword.
  protected Car(double distance) {
    this.distance = distance;
  }

  //compute velocity method
  public double computeVelocity(double time){
     return distance/time;
  }
}


public class RedCar extends Car{
  public double distance;

  public RedCar (double distance){
    // Call the Car constructor
    super(distance)
  }

  // No need to repeat the definition of 
  // computeVelocity unless you want to redefine 
  // the behaviour..
}

Then when you create a RedCar instance:

RedCar redCar = new RedCar(100.0);

first the RedCar constructor is called which then calls the Car constructor which sets the distance field to 100.0 .

You can call the method:

double velocity = redCar.computeVelocity(60.0);

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