Reputation: 45
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.
Upvotes: 3
Views: 3575
Reputation: 31
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);
}
Upvotes: 0
Reputation: 15304
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
}
Upvotes: 4