Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Confusion about inheritance

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?
    // 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;
    }
    }
    // 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:
    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:

    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.
    Last edited by MeteoricDragon; May 16th, 2012 at 06:03 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.
    Last edited by javapenguin; May 16th, 2012 at 05:50 PM.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default 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
    Last edited by aussiemcgr; May 16th, 2012 at 05:52 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confusion about inheritance

    Quote Originally Posted by aussiemcgr View Post
    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: Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    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?

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Confusion about inheritance

    Quote Originally Posted by MeteoricDragon View Post
    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    184
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default 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)
    Last edited by copeg; May 17th, 2012 at 06:35 PM. Reason: Clarification

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confusion about inheritance

    Quote Originally Posted by copeg View Post
    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.

Similar Threads

  1. Seaching an array confusion
    By ThuggedOut in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 19th, 2011, 06:43 PM
  2. Package confusion
    By caesius in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2011, 12:08 AM
  3. help 2d array confusion
    By Macgrubber in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 29th, 2010, 04:30 PM
  4. ArrayList confusion
    By Stormin in forum Collections and Generics
    Replies: 7
    Last Post: August 13th, 2010, 05:58 PM
  5. Confusion in creating class with OOPS concept
    By grbsmj in forum Object Oriented Programming
    Replies: 3
    Last Post: May 6th, 2009, 04:14 AM