Accessing member fields using interfaces

_vk_vk
edited August 2015 in Questions about Code

So I thought the code below would work, but it does not...

I'd like to hold some classes that implements an Interface in the same collection, but if I can't access member fields, then maybe it's useless... What am I doing wrong?

I i ;

void setup() {
  i = new C();
  // throws a cannot be resolved or is not a field
  println(i.x);
}

interface I {
}

class C implements I {
  int x;

  C() {
    x= 0;
  }
}
Tagged:

Answers

  • _vk_vk
    edited August 2015

    Humm using getters it works... Is this the way to go? Does my interface needs to have all getters?

    I i ;
    
    void setup() {
      i = new C();
      // works!
      println(i.getX());
    }
    
    interface I {
      int getX();
    }
    
    class C implements I {
      int x;
    
      C() {
        x= 0;
      }
    
      int getX(){
        return x;
      }
    }
    
  • edited August 2015 Answer ✓

    Jub. Iterfaces only allow you to define functions and constant variables, so if you don't want to use inheritance, setters and getters are the way to go.

    Well, if you need to access all variables via your interface, you kinda have to. Or you could spread the setters/getters over multiple interfaces:

    interface InterfaceA {
    
        int getA();
    
    }
    
    interface InterfaceB {
    
        float getB();
    
    }
    
    interface InterfaceC {
    
        String getC();
    
    }
    
    class ClassABC implements InterfaceA, InterfaceB, InterfaceC {
    
        int a;
        float b;
        String c;
    
        ClassABC() {
            a = 1;
            b = 2.0;
            c = "three";
        }
    
        int getA() {
            return a;
        }
    
        float getB() {
            return b;
        }
    
        String getC() {
            return c;
        }
    
    }
    
    void setup() {
        ClassABC classABC = new ClassABC();
        printA(classABC);
        printB(classABC);
        printC(classABC);
    
    }
    
    void printA(InterfaceA interfaceA) {
        println("A = " + interfaceA.getA());
    }
    
    void printB(InterfaceB interfaceB) {
        println("B = " + interfaceB.getB());
    }
    
    void printC(InterfaceC interfaceC) {
        println("C = " + interfaceC.getC());
    }
    
  • edited August 2015 Answer ✓
    • i.x fails b/c field i was declared as of datatype I.
    • And interface I doesn't have any field member called x there.
    • Even though the object i refers to is actually of datatype C...
    • Java language restricts the accessible members to the datatype a variable was declared as!
    • If you need to access C's member x from a variable of datatype I, you need to (cast) it as such:
      println( ((C) i).x );
    • Or store it in some more specific datatype variable: C c = (C) i; println(c.x);
  • Great guys. Thanks a lot.

  • _vk_vk
    edited August 2015

    @GoToLoop why is the extra parenthesis needed in line

    println( ((C) i).x);
    

    I mean this would be enough to cast

    ( C ) i
    

    right?

  • edited August 2015 Answer ✓

    The extra parens are needed b/c the operator dot . has a higher priority than the (cast)! @-)

  • Answer ✓

    @_vk: Because without the parenthesis ((C)i.x) the compiler would try to cast x to C.

  • edited August 2015 Answer ✓

    That is, w/o the extra parens, the (C) cast would act upon member x instead of i! >-)

  • Got it. Thanks!!

Sign In or Register to comment.