Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
subtype polymorphism possible in Gideros? — Gideros Forum

subtype polymorphism possible in Gideros?

grotlygrotly Member
edited September 2014 in Code snippets
How can I achieve in Gideros the same "effect" as in the pseudocode below? Is it even possible?
abstract class Animal {
    abstract String talk();
}
 
class Cat extends Animal {
    String talk() {
        return "Meow!";
    }
}
 
class Dog extends Animal {
    String talk() {
        return "Woof!";
    }
}
 
void letsHear(Animal a) {
    println(a.talk());
}
 
void main() {
    letsHear(new Cat());
    letsHear(new Dog());
}

Comments

  • asakharovasakharov Member
    edited September 2014 Accepted Answer
    Animal = Core.class()
    Cat = Core.class(Animal)
    Dog = Core.class(Animal)
     
    function Animal:talk()
      error("Override in derrived class!")
    end
     
    function Cat:talk()
      return "Meow!"
    end
     
    function Dog:talk()
      return "Woof!"
    end
     
    function letsHear(a)
        print(a:talk())
    end
     
    letsHear(Cat.new())
    letsHear(Dog.new())
  • Thanks, I wasn't sure if we can derive from our own classes. In the meantime, I also found the answer here

    http://giderosmobile.com/forum/discussion/15/oop-in-gideros/p1

Sign In or Register to comment.