This is my first time working with the java interface and im very confused. after reading tutorial online this is what i came up with how to define the interface and implement it but there is red underline on the hours,minutes and seconds. and i cant figure out why its so.
interface myClock {
int hours;
int minutes;
int seconds;
public void clock();
public void clock(int x, int y, int z);
public void setTime(int x, int y, int z);
public void incTimeBySec();
public void incTimeByMins(int x);
public void display12hr();
public void display24hr();
}
class time implements myClock {
public void clock() {
hours = 0;
minutes = 0;
seconds = 0;
}
public void clock(int x, int y, int z) {
hours = x;
minutes = y;
seconds = z;
}
public void setTime(int x, int y, int z) {
hours = x;
minutes = y;
seconds = z;
}
public void incTimeBySec() {
if(seconds+1>60) {
seconds = (seconds+1)-60;
minutes++;
hours++;
} else {
seconds+=1;
}
}
}
8 Answers 8
Interfaces only define methods. There are no data members, except for statics.
I'd recommend that you learn the Java coding standards. You aren't following them (e.g. class and interface names should be capitalized.)
public interface MyClock {
void setTime(int x, int y, int z);
void incTimeBySec();
void incTimeByMins(int x);
void display12hr();
void display24hr();
}
I would not recommend that you do this, unless you just want to learn about interfaces. The Java Date class implements all these methods better than you ever will. Use what's available to you.
Update: Since JDK 8 interfaces can have default implementations.
1 Comment
interfaces cannot have a state!
Comments
Instance variables, like hours, minutes and seconds here, are part of implementation details. An interface only specifies what methods the implementing classes should have, not how they should implement them. Therefore you cannot declare variables in interfaces. Constants declared using static final are allowed though.
Comments
All the fields defined in interface are by default public static final.
You are getting compile-time error because your you have not given values to the final fields of the interface while declaring them
Comments
All the variables defined in interface should be public static final
public static final int hours = 0;
public static final int minutes = 0;
public static final int seconds =0;
Ideally you can declare only constants
Comments
You can't declare instance variables in an interface. Move them into your implementation instead.
Comments
Error : The blank final field hours may not have been initialized Solution dont put variable in interface. only methods
public interface MyClock {
void setTime(int x, int y, int z);
void incTimeBySec();
void incTimeByMins(int x);
void display12hr();
void display24hr();
}
Comments
you do not initialization you'r variables , you can't do something like second++ when you declare second whit no initial value.
Interfaceare final by default, hence you need to define some value to them, likeint hours = 11, and you will access this field likeMyClock.hourseverywhere else :-)