interface & inheritance

edited February 2016 in Programming Questions

hi, is that an interface can have a field ? I've made this but I've get an error "[...] not have been initialized"

    interface theInterface {
      String name;
      String getName();
    }

    class ClassOne implements theInterface {
      ClassOne(String n) {
        name = n;
      }
      public String getName() {
        return name;
      }
    }

    /////////////////////////////////
    ClassOne instance0, instance1;
    void setup() {
      instance0 = new ClassOne("WhatAStrangeName");
      instance1 = new ClassOne("flowers");

      print(instance0.getName());
      exit();
    }

thanks

Answers

  • edited October 2013

    Field variables in Interfaces are both final & static by definition! 8-X
    So we gotta give them an immutable constant value, unfortunately! (~~)

  • Otherwise, you can use abstract classes, too.

  • thanks,

    I've made this, using interface & Superclass. can it be easier with abstract classes ?

    interface theInterface {
      String getName();
    }
    
    class SuperClass implements theInterface {
      String name;
      int xpos, ypos;
      SuperClass (String n) {
        name = n;
      }
      public String getName() {
        return name;
      }
    }
    
    class ClassOne extends SuperClass {
      ClassOne(String n) {
        super(n);
        }
      }
    class ClassTwo extends SuperClass {
      ClassTwo(String n) {
        super(n);
        }
      }
      /////////////////////////////////
      ClassOne instance0;
    ClassTwo instance01;
    void setup() {
      instance0 = new ClassOne("stone");
      instance01 = new ClassTwo("flowers");
    
      println(instance0.getName());
      println(instance01.getName());
      exit();
    }
    
  • edited October 2013 Answer ✓

    My take on it w/ abstract class in place of an interface: <):)

    // forum.processing.org/two/discussion/761/interface-inheritance
    
    final Abstract instance  = new SuperClass("Earth", 30, 75);
    
    final Abstract instance0 = new ClassOne("Stone", 10, 10);
    final Abstract instance1 = new ClassTwo("Flowers", 10, 50);
    
    void setup() {
      println(instance.getName() + "\t" + instance + "\n");
    
      println(instance0.getName() + "\t" + instance0);
      println(instance1.getName() + "\t" + instance1);
    
      // Anonymously instantiating an abstract class in loco:
      final Abstract anonymous = new Abstract() {
        {
          name = "Trees";
          xpos = -100;
          ypos = 75;
        }
    
        public String getName() {
          return name;
        }
      };
    
      println("\n" + anonymous.getName() + "\t" + anonymous);
    
      exit();
    }
    
    abstract class Abstract {
      // Fields in abstract classes are more flexible than 1s from interfaces.
      // They don't have to be final and static as the latter:
      String name;
      int xpos, ypos;
    
      // This 1 is abstract and demands implementation later on:
      abstract String getName();
    
      // This 1 isn't abstract and it's already implemented right here:
      String toString() {
        return "[" + xpos + ", " + ypos + "]";
      }
    }
    
    class SuperClass extends Abstract {
      // Utilizing inherited fields:
      SuperClass (String n, int x, int y) {
        name = n;
        xpos = x;
        ypos = y;
      }
    
      // Implementing inherited abstract method:
      String getName() {
        return name;
      }
    }
    
    class ClassOne extends SuperClass {
      ClassOne(String n, int x, int y) {
        super(n, x, y);
      }
    }
    
    class ClassTwo extends SuperClass {
      ClassTwo(String n, int x, int y) {
        super(n, x, y);
      }
    }
    
Sign In or Register to comment.