0

Why does the variable listed in the interface not show up in the class when I click implement methods? I look and I did not see the answer that I was really looking for, or would the variable be more like a property?

Class:

public class TestInterface implements myInterable {

    public TestInterface() {
    }

    public static void main(String[] args) {


    }

    @Override
    public void method1() {


    }

    @Override
    public void method2() {


    }
}

interface myInterable {
    public int score = 0;
    public void method1();
    public abstract void method2();
}

Tried this code but it would not work. It is almost exactly like the example on the listed Oracle tutorial.

Example:

interface TimeClient {
    void setTime(int hour, int minute, int second);
    void setDate(int day, int month, int year);
    void setDateAndTime(int day, int month, int year, int hour, int minute, int second);
    void getLocalDateTime();

    public static String getZoneId (String zoneString) { <-- Tutorial example
        return "";
    }

    public default int getZonedDateTime(String zoneString) { <-- like the tutorial
        return 0;
    }
}

Another Example:

Oracle states that you can not create an instance of an interface but if your class implements and interface than you can create a instance of the interface by create a new reference to the class? Is this how you would look at this. Is this a better way of creating an instance of a class by using an interface and putting all of you method stubs in the interface and then if you add in a new interface method than you do not have to rewrite all of your code because of one or two new methods.

If this is correct and I want to add a new method to my say application I would not have to go through and recompile every class to add in one new method if my variable uses this type of instance creation.

public class TestInterface implements myInterable,
                                      Runnable,
                                      MouseListener {
     //Code goes here
}

public myInterable uu = new TestInterface(); <--This is the interface class

Example that does not work.

Should this work if I create an instance of the interface with the class variable name.

public class TestInterface implements myInterable,
                                      Runnable,
                                      MouseListener {

    public  myInterable uu = new TestInterface();

    public TestInterface() {
    }

    public void method1() {
    }


    public void method2() {
    }


    public void run() {
        System.out.println("Runnable");
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }

    public static void main(String[] args) {
        TestInterface b = new TestInterface();
        b.run();
        b.uu.run();
    }
}

interface myInterable {
    public int score = 0;
    public void method1();
    public abstract void method2();
    public abstract void run();
//     void method3() {
//      int index = 0;
//       for (index = 0; index < 10; index++) {
//       }
//  }
}

Error Message:

at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
at DDHIntefaceExample.TestInterface.<init>(TestInterface.java:10)
Doug Hauf
  • 3,025
  • 8
  • 46
  • 70
  • possible duplicate of [how can I declare java interface field that implement class should refine that field](http://stackoverflow.com/questions/6543243/how-can-i-declare-java-interface-field-that-implement-class-should-refine-that-f) – Mike Dinescu Jan 21 '14 at 02:19
  • @Doug: I think your confusion is regarding what an interface is supposed to be. It's meant to declare an API, a contract, a set of operations. You are confusing interfaces with abstract classes I think. They are two separate concepts. Typically, if you want to have implementations inherited by child classes, you would first define an interface that represents the concept you're trying to model, then create an abstract class that implements that interface, providing the default implementations - finally declare child to classes to extend that abstract class, overriding where necessary. – aberrant80 Jan 22 '14 at 08:25
  • i read in a book, however, that with OOP programming that it is a good idea to make your variables with the interface instead of the class because it fits in more with the OOP concept of programming. – Doug Hauf Jan 22 '14 at 13:44

3 Answers3

5

The JLS states

Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.

Therefore, the field score in your example is not inherited since it is static.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • It can be used just like any other variable but is constant correct? – Doug Hauf Jan 21 '14 at 13:55
  • @doug depends what you mean by _like any other variable_. You can access it statically through a reference to the interface type. Because it is final you cannot reassign it. – Sotirios Delimanolis Jan 21 '14 at 14:06
  • @DougHauf In an interface, a declaration like `public int a = 0;` is actually `public static final int a = 0;`. – Sotirios Delimanolis Jan 22 '14 at 02:15
  • So a variable in an interface is always static final. When I see these in a lot of examples I find that most leave this off for the most part. – Doug Hauf Jan 22 '14 at 02:35
  • @DougHauf Yes, you also cannot make the variable `protected` or `private`. If you omit the access modifier, it won't be default like in field declarations in classes, it will be public. It's just easier to write `public int a = 0;`. – Sotirios Delimanolis Jan 22 '14 at 02:37
2

You can only define constants or static variables in interfaces, not instance variables. if you want to define an instance variable, why not use an abstract class and provide getters and setters and make the instance variable private?

2

Just to nitpick, your myInterable should be MyInterable, following the typical Java capitalisation conventions.

All methods in an interface are inherently public abstract (and you cannot define static methods). Java 8 may change this though.

All variables in an interface are inherently public static final.

For much more details, see Java Tutorial trail on interfaces

So your

interface MyInterable {
    public int score = 0;
    public void method1();
    public abstract void method2();
}

Is the same as

interface MyInterable {
    public static final int score = 0;
    public void method1();
    public void method2();
}

And is also the same as

interface MyInterable {
    int score = 0;
    void method1();
    void method2();
}
aberrant80
  • 12,815
  • 8
  • 45
  • 68
  • 1
    Don't forget the `final`. – Sotirios Delimanolis Jan 21 '14 at 02:53
  • Where does the final go? – Doug Hauf Jan 21 '14 at 13:54
  • I read in a book that it is better to create a variable of data type of an interface than of a class? When and where would you do this? – Doug Hauf Jan 21 '14 at 13:59
  • @doug the variable is implicitly final. – Sotirios Delimanolis Jan 21 '14 at 14:07
  • Can yo examplain that a little more or even show a code exmaple. If I declare a variabel it is final like this. myInterface a = new myInterface(); -- final in this instance. If I add more to the interface down the road then i do not have to change anything in my code because my variable will have access to even the new methods even if they are create with default or static and do supply some code. Because interface appear to be able to supply code as well in some cases. This I had not seen before. Doug – Doug Hauf Jan 21 '14 at 15:36
  • @Doug "final" refers to the fact that you cannot reassign the variable to another instance during runtime. It doesn't mean that you can modify the interface class when you're typing it (unless I'm misunderstanding your question). You're confusing runtime with compile time. – aberrant80 Jan 22 '14 at 08:20