Reputation: 345
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;
}
}
}
Upvotes: 1
Views: 3400
Reputation: 308763
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.
Upvotes: 7
Reputation: 125
you do not initialization you'r variables , you can't do something like second++ when you declare second whit no initial value.
Upvotes: 0
Reputation: 6537
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();
}
Upvotes: 0
Reputation: 111259
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.
Upvotes: 2
Reputation: 3386
You can't declare instance variables in an interface. Move them into your implementation instead.
Upvotes: 1
Reputation: 5531
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
Upvotes: 1
Reputation: 17622
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
Upvotes: 1