Confusion about inheritance
I am reading a SCJA study guide and it's explaining examples of classes that use inheritance, I saw one example and am confused why the wheelRPM variable is overriden, even with the explanation given below, it inherits the variable so why can't it be set by the methods of the TenSpeedBicycle class? Can anyone help me understand?
Code java:
// This is the base class, it is a basic bicycle
public class Bicycle
{
private float wheelRPM;
private int degreeOfTurn;
public void pedalRPM(float pedalRPM)
{
float gearRatio = 2f;
this.wheelRPM = pedalRPM * gearRatio;
}
public void setDegreeOfTurn(int degreeOfTurn){
this.degreeOfTurn = degreeOfTurn;
}
public float getWheelRPM()
{
return this.wheelRPM;
}
public int getDegreeOfTurn()
{
return this.degreeOfTurn;
}
}
Code java:
// This is the subclass, it is a special kind of bicycle.
public class TenSpeedBicycle extends Bicycle
{
private float gearRatio = 2f;
private float wheelRPM; // why are we overriding this variable? It is inherited, no?
public void setGearRatio(float gearRatio)
{
this.gearRatio = gearRatio;
}
public void pedalRPM(float pedalRPM)
{
this.wheelRPM = pedalRPM * gearRatio;
}
public float getWheelRPM()
{
return this.wheelRPM;
}
}
This is a quote from the book:
Quote:
The TenSpeedBicycle class, listed in the preceding example, extends the
Bicycle class. This class represents a bicycle that has ten different possible gear
ratios. The regular Bicycle class cannot be used because it has a fixed gear ratio.
The TenSpeedBicycle class adds a method and instance variable so a gear ratio
can be set. It also overrides the wheelRPM variable. This must be done because the
Bicycle class has no setter to set that variable directly. The TenSpeedBicycle
class also overrides the pedalRPM(float pedalRPM) method. In the Bicycle
class version of this method, the gear ratio was fixed. In the newer version, it uses
the gear ratio that can be set. To retrieve the wheelRPM variable, the getter must
also be overridden. This is because the original version of this method can only
return the instance variable that is in its same class.
ok, and to go through what I don't get:
Quote:
It also overrides the wheelRPM variable.This must be done because the
Bicycle class has no setter to set that variable directly.
To which I say, what does 'the Bicycle class has no setter to set that variable directly' have to do with the TenSpeedBicycle class's ability to access the wheelRPM variable, inherited from the Bicycle Class? It has a private access modifier, meaning any Bicycle objects can access their own wheelRPM variables and any TenSpeedBicycle objects created inherit this member that they can access privately too.
Re: Confusion about inheritance
Not sure what you mean exactly, but you can't override private variables. In fact, you can't inherit them either.
It appears actually to be overriding pedalRPM.
Re: Confusion about inheritance
private variables and methods are not directly inherited by subclasses, only public and protected variables and methods are. However, a subclass can access it's uninherited variables by using public or protected setters and getters created in the superclass.
EDIT: This is a nice link that may help: http://docs.oracle.com/javase/tutori...sscontrol.html
Re: Confusion about inheritance
Quote:
Originally Posted by
aussiemcgr
That link does help, but I am also confused between inheritance and access given by protected access modifiers. If I create a Bicycle object with protected wheelRPM, then I create a TenSpeedBicycle object that extends Bicycle, can the TenSpeedBicycle object access the Bicycle object's protected wheelRPM variable directly?
Re: Confusion about inheritance
Quote:
Originally Posted by
MeteoricDragon
That link does help, but I am also confused between inheritance and access given by protected access modifiers. If I create a Bicycle object with protected wheelRPM, then I create a TenSpeedBicycle object that extends Bicycle, can the TenSpeedBicycle object access the Bicycle object's protected wheelRPM variable directly?
Yes. Protected variables and protected methods get directly inherited.
Re: Confusion about inheritance
Quote:
Originally Posted by javapenguin
In fact, you can't inherit them either.
Quote:
Originally Posted by aussiemcgr
private variables and methods are not directly inherited by subclasses, only public and protected variables and methods are. However, a subclass can access it's uninherited variables by using public or protected setters and getters created in the superclass.
I must interject here and make a clarification in reference to the above statements
Access modifiers do not necessarily define what is inherited - they define what can be accessed/overridden in child classes. One might argue if you cannot access something in a child class than you don't inherit - but I'd make the argument that just because you cannot access it does not mean you do not inherit its behavior. This in fact is a big distinction, because inheritance would not be inheritance if you could not inherit behavior defined by parent classes (regardless of how that behavior can be accessed/overridden from outside a class)
Re: Confusion about inheritance
Quote:
Originally Posted by
copeg
I must interject here and make a clarification in reference to the above statements
Access modifiers do not necessarily define what is inherited - they define what can be accessed/overridden in child classes. One might argue if you cannot access something in a child class than you don't inherit - but I'd make the argument that just because you cannot access it does not mean you do not inherit its behavior. This in fact is a big distinction, because inheritance would not be inheritance if you could not inherit behavior defined by parent classes (regardless of how that behavior can be accessed/overridden from outside a class)
Thank you copeg, this was the more articulate way I sought to express my confusion. and helped me understand, thank you all.