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);
}
}