简体   繁体   中英

Calling Super class Constructor

I have an interesting problem, maybe I'm approaching this problem wrong but I feel that I'm on the right track. I am trying to make a superclass that my subclasses can reference. So I have most of the code setup but can't figure out the default constructor here is the error I'm getting:

Cannot reference 'GregorianDate.day' before supertype constructor has been called.

public class GregorianDate extends Date {
    //Initialise the variables
    public int month = 1;
    public int day = 1;
    public int year = 1970;


    //*************** Constructors ***********************
    GregorianDate() {
        super(month,day,year);
        long numToAdd = System.currentTimeMillis();
        numToAdd += java.util.TimeZone.getDefault().getRawOffset();
        numToAdd /= 86400000;
        super.addDays(numToAdd);
    }

    //Parameterized constructor
    GregorianDate(int passedYear, int passedMonth, int passedDay){
        super(passedMonth, passedDay, passedYear);
    }

SuperClass file:

public class Date {
    public int month;
    public int day;
    public int year;

    Date(int passedMonth, int passedDay, int passedYear){
       month = passedMonth;
       day = passedDay;
       year = passedYear;
    }

I've tried adding a default constructor with nothing and calling it by super(); with the same result. Any help is appreciated.

A few problems with your code.

First, as the error suggest, you cannot use the member variable before the super call because the object has not been fully initialized yet. Second, your subclass shouldn't have same public variables as the superclass. Third, a common practice in Java is to use getter/settter instead of public variables.

Here is my version of your code:

public class GregorianDate extends Date {
    //Define constants
    private final static int MONTH = 1;
    private final static int DAY = 1;
    private final static int YEAR = 1970;


    //*************** Constructors ***********************
    GregorianDate() {
        super(MONTH,DAY,YEAR);
        long numToAdd = System.currentTimeMillis();
        numToAdd += java.util.TimeZone.getDefault().getRawOffset();
        numToAdd /= 86400000;
        super.addDays(numToAdd);
    }

    //Parameterized constructor
    GregorianDate(int passedYear, int passedMonth, int passedDay){
        super(passedMonth, passedDay, passedYear);
    }

    //getters and setters here
}

SuperClass file:

public class Date {
    private int month;
    private int day;
    private int year;

    Date(int passedMonth, int passedDay, int passedYear){
       this.month = passedMonth;
       this.day = passedDay;
       this.year = passedYear;
    }

    //Getters and setters here
}

Because day,month,year are instance variable of Gregorian class. So they can be available only after class getting instantiate. So in your case compiler won't be able to find those value while calling parent class.

So in your empty constructor not receiving any values of day,month,year so throw an compilation error

I think you have to call super constructor first.

You can try it

public class Date {
public int month;
public int day;
public int year;

Date()
Date(int passedMonth, int passedDay, int passedYear){
   month = passedMonth;
   day = passedDay;
   year = passedYear;
}

Then use it first in the constructor before your class constructor.

public class GregorianDate extends Date {
//Initialise the variables
public int month = 1;
public int day = 1;
public int year = 1970;


//*************** Constructors ***********************


GregorianDate() {
    super(month,day,year);
    long numToAdd = System.currentTimeMillis();
    numToAdd += java.util.TimeZone.getDefault().getRawOffset();
    numToAdd /= 86400000;
    super.addDays(numToAdd);
}

//Parameterized constructor
GregorianDate(int passedYear, int passedMonth, int passedDay){
    super(passedMonth, passedDay, passedYear);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM