1

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;
 }
 }
}
nIcE cOw
24.6k8 gold badges54 silver badges147 bronze badges
asked Sep 8, 2013 at 17:12
1
  • 2
    Fields defined inside an Interface are final by default, hence you need to define some value to them, like int hours = 11, and you will access this field like MyClock.hours everywhere else :-) Commented Sep 8, 2013 at 17:19

8 Answers 8

7

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.

answered Sep 8, 2013 at 17:15
Sign up to request clarification or add additional context in comments.

1 Comment

It's not true. Interfaces can contain data. That's what he asked for.
5

interfaces cannot have a state!

answered Sep 8, 2013 at 17:14

Comments

2

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.

answered Sep 8, 2013 at 17:18

Comments

1

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

answered Sep 8, 2013 at 17:16

Comments

1

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

answered Sep 8, 2013 at 17:16

Comments

1

You can't declare instance variables in an interface. Move them into your implementation instead.

answered Sep 8, 2013 at 17:16

Comments

0

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();
}
answered Sep 8, 2013 at 17:19

Comments

0

you do not initialization you'r variables , you can't do something like second++ when you declare second whit no initial value.

answered Sep 8, 2013 at 17:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.