CSC/ECE 517 Fall 2007/wiki1 6 b2
An example
In Java, we may define “say” method as interface in Staff and Student and then implement them in class WorkStudy.
Java solution
Staff.java public interface staff { void saySta(); } Student.java public interface student { void sayStu(); } WorkStudy.java public class WorkStudy implements staff, student{ private String name; WorkStudy(String name){ this.name=name; } public void saySta(){ System.out.println(name+" is working here!"); } public void sayStu(){ System.out.println(name+" is studying here!"); } public void say(){ saySta(); sayStu(); System.out.println("So, "+name+" is work-study student here!"); } } mainclass.java public class mainclass { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub WorkStudy j; j=new WorkStudy("Ying"); j.say(); } }
Comparision with Java
Comparing with java, mixins has two advantages.
Firstly, mixins is like a “mix” of interface and abstract class. Interface is designed in Java to perform the act similar to “multiple inheritance” in C++. It is so purely abstract that either any implement of method or instant variable is not allowed. Meanwhile, abstract class may contain abstract method and non-abstract method but one class can only inherited from one super abstract class. Mixin here provides a more flexible way between them. We may implement methods in module or in class inherited from the module to reduce the code redundancy.
Secondly, the namespace provided by module helps dealing with the situation that a class inherits two methods from different modules with the same name. In Java, such act will throw out an error message.