Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By benji2505

Thread: Inheritance (still not seen from the outside)

  1. #1
    joodez is offline Member
    Join Date
    Dec 2015
    Posts
    1
    Rep Power
    0

    Default Inheritance (still not seen from the outside)

    I'm building a class that needs to inherit data from another class within the same hierarchy. I want specifically to use these two fields:

    Java Code:
    // Data
        private int numerator; // holds numerator
        private int denominator; // holds denominator
    Specifically, I need to maintain that the fields can't be seen from the outside (not necessarily sure what this means), while still being available to use within another subclass. Is there a specific way I have to write the subclass?

    Java Code:
    /package fraction6;
     
    import javax.swing.*;
    import java.util.*;
     
    public class WholeNumber extends Fraction
    {
            // sets the numerator to 0 and denominator to 1.
    	public WholeNumber()
    	{
               numerator = 0;
               denominator = 1;
    	}
     
    }
    it's giving me an error when I call both the numerator and denominator, presumably because they're not available yet?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Inheritance (still not seen from the outside)

    giving me an error
    Please copy the full text of the error messages and paste it here. The messages should say what the problem is.

    Take a look here: http://docs.oracle.com/javase/tutori...sscontrol.html
    Last edited by Norm; 12-12-2015 at 06:03 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Inheritance (still not seen from the outside)

    Private variables and methods are not visible from subclasses. Check out the tutorials in my signature and
    search the index for controlling access to members of a class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: Inheritance (still not seen from the outside)

    you can encapsulate the private variables in the super class and acces them via the setters. That would be my preferred version. The getters and setters are visible from the outside, but the variables are not.

    alternatively you can create a constructor for the super class that takes the requested variables as parameters and then call the constructor in the subclass with the parameters: super( parameter1, parameter2).
    jim829 likes this.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Inheritance (still not seen from the outside)

    Quote Originally Posted by benji2505 View Post
    you can encapsulate the private variables in the super class and access them via the setters. That would be my preferred version. The getters and setters are visible from the outside, but the variables are not.
    Actually, that is an especially good idea since the access methods hide the implementation details of how the fields are represented. And using just a getter can provide a level of immutability.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. inheritance
    By FREDISCO in forum New To Java
    Replies: 2
    Last Post: 03-27-2012, 03:11 AM
  2. using inheritance
    By droidus in forum New To Java
    Replies: 1
    Last Post: 10-18-2011, 03:38 AM
  3. Inheritance
    By Nerijus in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 04:47 AM
  4. Inheritance
    By gpio in forum Advanced Java
    Replies: 14
    Last Post: 11-20-2009, 11:41 AM
  5. inheritance
    By itaipee in forum New To Java
    Replies: 6
    Last Post: 01-20-2009, 09:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •