UNC-CH COMP 590-059

One: Elixir OO Problem

Write a program in Elixir that implements "objects".

Here is a Java program to define the behavior we want the Elixir code to have:

   abstract class Animal {
     private String name;              // encapsulation

     public Animal(String name) { this.name = name; }

     public String getName() { return name; }   // encapsulation

     public abstract void speak();   // abstraction + polymorphism
   }

   class Dog extends Animal {
     public Dog(String name) { super(name); }

     @Override
     public void speak() { System.out.println(getName() + " says: Woof!"); }
   }

   class Cat extends Animal {
     public Cat(String name) { super(name); }

     @Override
     public void speak() { System.out.println(getName() + " says: Meow!"); }
   }

   public class Main {
     public static void main(String[] args) {
       Animal a1 = new Dog("Rex");
       Animal a2 = new Cat("Whiskers");
  
       a1.speak(); // polymorphism
       a2.speak();
     }
   }

Your goal is to design and write Elixir code that provides the same OO "pillars" (encapsulation, abstraction, inheritance, and polymorphism) that Java does, but in a functional, actor-based language with no objects, no classes, and no class-based inheritance.

In your Elixir, for processes use the basic spawn_link approach, and do not use higher level packages like GenServer, etc. Just use the basic Elixir language features.

Questions for Discussion (readme file)