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 2 of 2

Thread: Accessing Private Int in a Subclass

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Accessing Private Int in a Subclass

    Superclass source code
    public class Date {
    private int month;
    private int day;
    private int year;


    public Date() {
    setMonth(1);
    **day = 1;**
    setYear(1900);
    }

    public Date(int month, int day, int year) {
    this.setMonth(month);
    this.**day** = day;
    this.setYear(year);
    }

    Month and Year works fine because I can use the setMonth and setYear in my subclass. However, when I try to use day it says the var is not visible because its private. There is no setter for day in the superclass but there is a getter. What should the setter look like? Furthermore, what should my subclass contructor look like?

    Subclass contructor
    public EDate(int month, int day, int year)
    {

    this.setMonth(month);
    day = getDay();
    this.setYear(year);
    }

    Subclass Day setter
    public void setDay(int newInt) {
    if (isGooddDate(getMonth(), newInt, getYear())==true)
    {
    newInt = this.getDay();
    }

    Any help is much appreciated!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Accessing Private Int in a Subclass

    Please post the full code and the full text of the error message you are getting.
    Be sure to wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Cannot find methods in a subclass
    By Dirk94 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 15th, 2012, 08:32 PM
  2. Accessing fields of a subclass
    By Shaybay92 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 2nd, 2011, 11:41 AM
  3. [SOLVED] how to extend a subclass?!
    By migongotar in forum Object Oriented Programming
    Replies: 4
    Last Post: August 2nd, 2011, 03:22 PM
  4. subclass that implements
    By speedycerv in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2011, 07:35 AM
  5. Instantiating a SuperClass Map from a SubClass
    By drexasaurus in forum Collections and Generics
    Replies: 1
    Last Post: September 9th, 2010, 10:51 PM